관리 메뉴

Kim's Programming

Algorithm - is_sorted() 본문

STL - Algorithm/Algorithm - Sorting

Algorithm - is_sorted()

Programmer. 2017. 6. 30. 13:18

원본)


1
2
3
4
5
6
7
//기본형
template <class ForwardIterator>
  bool is_sorted (ForwardIterator first, ForwardIterator last);
 
//사용자 정의형
template <class ForwardIterator, class Compare>
  bool is_sorted (ForwardIterator first, ForwardIterator last, Compare comp);
cs



의미)


[Iterator first, Iterator last)사이에 있는 데이터들이 오름차순으로 정렬이 되어있으면 true를 리턴합니다.



소스)


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#include <iostream>
#include <algorithm>
#include <vector>
 
void Print(const std::vector<int>& target)
{
    for (std::vector<int>::const_iterator iterPos = target.cbegin(); iterPos != target.cend(); iterPos++)
        std::cout << *iterPos << " ";
    std::cout << std::endl;
}
 
void main()
{
    std::vector<int> vector = { 255,15,10,30,20,15 };
    Print(vector);
    if (std::is_sorted(vector.begin(), vector.end()))
        std::cout << "sorted!" << std::endl;
    else
        std::cout << "not sorted!" << std::endl;
    std::sort(vector.begin(), vector.end());
    Print(vector);
    if (std::is_sorted(vector.begin(), vector.end()))
        std::cout << "sorted!" << std::endl;
    else
        std::cout << "not sorted!" << std::endl;
}
cs



내용)


[Iterator first, Iterator last)사이에 있는 데이터들이 오름차순으로 정렬이 되어있다면 true를 리턴하고 그 외의 경우 false를 리턴합니다.



결과)





'STL - Algorithm > Algorithm - Sorting' 카테고리의 다른 글

Algorithm - is_sorted_until()  (0) 2017.06.30
Algorithm - partial_sort_copy()  (0) 2017.06.30
Algorithm - partial_sort()  (0) 2017.06.30
Algorithm - stable_sort()  (0) 2017.06.30
Algorithm - sort()  (0) 2017.06.30