STL - Algorithm/Algorithm - Other
Algorithm - lexicographical_compare()
Programmer.
2017. 6. 21. 13:31
원형)
1 2 3 4 5 6 7 8 9 10 | //기본형 template <class InputIterator1, class InputIterator2> bool lexicographical_compare(InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, InputIterator2 last2); //사용자 정의형 template <class InputIterator1, class InputIterator2, class Compare> bool lexicographical_compare(InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, InputIterator2 last2, Compare comp); | cs |
의미)
first1(포함)부터 last1 바로 앞까지와 first2(포함)부터 last2 비교하여 first2~last2사이의 데이터들이 사전적으로 first1~last1 사이의 데이터들보다 뒤에 있다면 true를 리던합니다.
소스)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | #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() { char string1[] = "programming"; char string2[] = "stl"; std::cout << std::lexicographical_compare(string1, string1 + 11, string2, string2 + 3) << std::endl; } | cs |
리턴 값)
first2 ~ last 2 데이터가 더 앞에 있다면 truefmf 리턴하고 같은경우를 포함 그외의 경우들은 false를 리턴합니다.
결과)