类别: 算法 | 组件类型: 函数 |
template <class InputIterator1, class InputIterator2> bool lexicographical_compare(InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, InputIterator2 last2); template <class InputIterator1, class InputIterator2, class BinaryPredicate> bool lexicographical_compare(InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, InputIterator2 last2, BinaryPredicate comp);
这两个版本字典序比较在如何定义一个元素小于另一个元素方面有所不同。第一个版本使用operator<比较对象,第二个版本使用一个函数对象comp.
int main() { int A1[] = {3, 1, 4, 1, 5, 9, 3}; int A2[] = {3, 1, 4, 2, 8, 5, 7}; int A3[] = {1, 2, 3, 4}; int A4[] = {1, 2, 3, 4, 5}; const int N1 = sizeof(A1) / sizeof(int); const int N2 = sizeof(A2) / sizeof(int); const int N3 = sizeof(A3) / sizeof(int); const int N4 = sizeof(A4) / sizeof(int); bool C12 = lexicographical_compare(A1, A1 + N1, A2, A2 + N2); bool C34 = lexicographical_compare(A3, A3 + N3, A4, A4 + N4); cout << "A1[] < A2[]: " << (C12 ? "true" : "false") << endl; cout << "A3[] < A4[]: " << (C34 ? "true" : "false") << endl; }