관리 메뉴

Kim's Programming

Algorithm - prev_permutation() 본문

STL - Algorithm/Algorithm - Other

Algorithm - prev_permutation()

Programmer. 2017. 6. 21. 13:45

원형)


1
2
3
4
5
6
7
8
9
//기본형
template <class BidirectionalIterator>
  bool next_permutation (BidirectionalIterator first,
                         BidirectionalIterator last);
 
//사용자 정의형
template <class BidirectionalIterator, class Compare>
  bool next_permutation (BidirectionalIterator first,
                         BidirectionalIterator 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
#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 = { 2,3,4,};
    std::random_shuffle(vector.begin(), vector.end());
 
    do
    {
        Print(vector);
    } while (std::prev_permutation(vector.begin(), vector.end()));
}
cs


리턴 값)


오름차순으로 정렬이 끝난 상태인경우 true를 그 이외의 경우에는 false를 리턴합니다.



결과)




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

Algorithm - next_permutation()  (0) 2017.06.21
Algorithm - lexicographical_compare()  (0) 2017.06.21