C++ algorithm
기타 algorithm에 속한 다른 함수들을 알아본다.
1. sort(begin, end)
구간 [begin, end)를 오름차순 기준으로 정렬한다
sort 사용 예제
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 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 | #include <iostream> // std::cout #include <algorithm> // std::sort #include <vector> // std::vector #include <functional> bool myfunction(int i, int j) { return (i>j); } struct myclass { bool operator() (int i, int j) { return (i<j); } } myobject; int main() { int myints[] = { 32,71,12,45,26,80,53,33 }; std::vector<int> myvector(myints, myints + 8); // 32 71 12 45 26 80 53 33 // using default comparison (operator <): std::sort(myvector.begin(), myvector.end()); //(12 26 32 33 45 53 71 80) std::cout << "myvector contains:"; for (std::vector<int>::iterator it = myvector.begin(); it != myvector.end(); ++it) std::cout << ' ' << *it; std::cout << '\n'; // using function as comp std::sort(myvector.begin(), myvector.end(), myfunction); // (80 71 53 45 33 32 26 12) std::cout << "myvector contains:"; for (std::vector<int>::iterator it = myvector.begin(); it != myvector.end(); ++it) std::cout << ' ' << *it; std::cout << '\n'; // using object as comp std::sort(myvector.begin(), myvector.end(), myobject); //(12 26 32 33 45 53 71 80) // print out content: std::cout << "myvector contains:"; for (std::vector<int>::iterator it = myvector.begin(); it != myvector.end(); ++it) std::cout << ' ' << *it; std::cout << '\n'; // using function as greater std::sort(myvector.begin(), myvector.end(), std::greater<int>()); // (80 71 53 45 33 32 26 12) std::cout << "myvector contains:"; for (std::vector<int>::iterator it = myvector.begin(); it != myvector.end(); ++it) std::cout << ' ' << *it; std::cout << '\n'; return 0; } | cs |

기본은 오름차순 정렬이며
내림차순으로 정렬하기 위햇서는 비교 함수를 구현하거나
<functional> 헤더를 추가하여 41번째 줄과 같이 std::greater<int>() 를 이용해서 비교하면 내림차순으로 정렬할 수 있다.
2. stable_sort(begin, end)
sort와 마찬가지로 정렬하는 함수이다. 차이점은 비교하여 같은 값을 가진 때, 원래 앞에 있던 값이 그대로 앞에 위치하게 된다. 아래 예제를 보자.
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 27 28 29 30 31 32 | #include <iostream> // std::cout #include <algorithm> // std::stable_sort #include <vector> // std::vector bool compare_as_ints(double i, double j) { return (int(i)<int(j)); } int main() { double mydoubles[] = { 3.14, 1.41, 2.72, 4.67, 1.73, 1.32, 1.62, 2.58 }; std::vector<double> myvector; myvector.assign(mydoubles, mydoubles + 8); std::cout << "using default comparison:"; std::stable_sort(myvector.begin(), myvector.end()); for (std::vector<double>::iterator it = myvector.begin(); it != myvector.end(); ++it) std::cout << ' ' << *it; std::cout << '\n'; myvector.assign(mydoubles, mydoubles + 8); std::cout << "using 'compare_as_ints' :"; std::stable_sort(myvector.begin(), myvector.end(), compare_as_ints); for (std::vector<double>::iterator it = myvector.begin(); it != myvector.end(); ++it) std::cout << ' ' << *it; std::cout << '\n'; return 0; } | cs |
위 예제에서 비교 연산자없이 stable_sort를 호출하면, 오름차순으로 정렬된다.
하지만 비교 조건으로 int로 변환했을 때(정수부의 값이)를 기준으로 오름차순 정렬하도록 조건을 주었다.
이런 경우 1.41 1.73 1.32 1.62가 모두 1의 값을 가지게 되는데, 이럴 때, 원래 가지고 있던 순서 그대로 위치하게 된다.

3. binary_search(begin, end, value)
정렬된 구간 [begin, end)에서 이진 탐색으로 value를 찾으면 true반환, 못찾으면 false를 반환한다.
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> // std::cout #include <algorithm> // std::binary_search, std::sort #include <vector> // std::vector bool myfunction(int i, int j) { return (i<j); } int main() { int myints[] = { 1,2,3,4,5,4,3,2,1 }; std::vector<int> v(myints, myints + 9); // 1 2 3 4 5 4 3 2 1 // using default comparison: std::sort(v.begin(), v.end()); std::cout << "looking for a 3... "; if (std::binary_search(v.begin(), v.end(), 3)) std::cout << "found!\n"; else std::cout << "not found.\n"; // using myfunction as comp: std::sort(v.begin(), v.end(), myfunction); std::cout << "looking for a 6... "; if (std::binary_search(v.begin(), v.end(), 6, myfunction)) std::cout << "found!\n"; else std::cout << "not found.\n"; return 0; } | cs |

4. lower_bound(begin, end, value)
구간 [begin, end)에서 value보다 작지 않은 첫 번째 이터레이터 반환
5. upper_bound(begin, end, value)
구간 [begin, end)에서 value보다 큰 첫 번째 이터레이터 반환
사용 예제
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | #include <iostream> // std::cout #include <algorithm> // std::lower_bound, std::upper_bound, std::sort #include <vector> // std::vector int main() { int myints[] = { 10,20,30,30,20,10,10,20 }; std::vector<int> v(myints, myints + 8); // 10 20 30 30 20 10 10 20 std::sort(v.begin(), v.end()); // 10 10 10 20 20 20 30 30 std::vector<int>::iterator low, up; low = std::lower_bound(v.begin(), v.end(), 20); // ^ up = std::upper_bound(v.begin(), v.end(), 20); // ^ std::cout << "lower_bound at position " << (low - v.begin()) << '\n'; std::cout << "upper_bound at position " << (up - v.begin()) << '\n'; return 0; } | cs |

6. next_permutation(begin, end)
구간 [begin, end)를 순열로 보았을 때, 사전 순으로 다음에 오는 순열을 만든다.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | #include <iostream> // std::cout #include <algorithm> // std::next_permutation, std::sort int main() { int myints[] = { 1,2,3 }; std::sort(myints, myints + 3); std::cout << "The 3! possible permutations with 3 elements:\n"; do { std::cout << myints[0] << ' ' << myints[1] << ' ' << myints[2] << '\n'; } while (std::next_permutation(myints, myints + 3)); std::cout << "After loop: " << myints[0] << ' ' << myints[1] << ' ' << myints[2] << '\n'; return 0; } | cs |

7. heap
make_heap: 힙을 생성한다. (최대힙)
push_heap: 힙에 원소를 추가한다.
pop_heap: 힙에서 원소를 제거한다.
sort_heap: 힙을 정렬한다.
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 27 | #include <iostream> // std::cout #include <algorithm> // std::make_heap, std::pop_heap, std::push_heap, std::sort_heap #include <vector> // std::vector int main() { int myints[] = { 10,20,30,5,15 }; std::vector<int> v(myints, myints + 5); std::make_heap(v.begin(), v.end()); std::cout << "initial max heap : " << v.front() << '\n'; std::pop_heap(v.begin(), v.end()); v.pop_back(); std::cout << "max heap after pop : " << v.front() << '\n'; v.push_back(99); std::push_heap(v.begin(), v.end()); std::cout << "max heap after push: " << v.front() << '\n'; std::sort_heap(v.begin(), v.end()); std::cout << "final sorted range :"; for (unsigned i = 0; i<v.size(); i++) std::cout << ' ' << v[i]; std::cout << '\n'; return 0; } | cs |

참조: http://www.cplusplus.com/reference/algorithm/
참고문헌: 뇌를 자극하는 C++ STL