首页 > STL编程 > STL lexicographical_compare

STL lexicographical_compare

先看SGI STL的解释

Lexicographical_compare returns true if the range of elements [first1, last1) is lexicographically less than the range of elements [first2, last2), and false otherwise. Lexicographical comparison means "dictionary" (element-by-element) ordering. That is, [first1, last1) is less than [first2, last2) if *first1 is less than *first2, and greater if *first1 is greater than *first2. If the two first elements are equivalent then lexicographical_compare compares the two second elements, and so on. As with ordinary dictionary order, the first range is considered to be less than the second if every element in the first range is equal to the corresponding element in the second but the second contains more elements.

大概意思就是说:

lexicographical_compare返回true,当且仅当区间[first1, last1)里面的所有元素在字典序上都小于区间[first2, last2)里面的所有元素,否则返回false。lexicographical比较也就是字典序比较的意思(逐个元素比较)。也就是说,当*first1小于*first2时,[first1, last1)小于[first2, last2);反之当*first1大于*first2时,[first1, last1)大于[first2, last2)。如果两个元素相等的话,则继续比较下一个元素,以此类推。和通常的字典序一样,如果第一个区间的元素和第二个区间对应的元素相等,但是第一个区间的元素个数比第二个区间的元素个数少,那么第一个区间小于第二个区间。

复杂度:线性复杂度,最多2 * min(last1 – first1, last2 – first2)次比较。

 

函数声明:(两个版本)

 

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);


觉得文章还不错?点击此处对作者进行打赏!


本文地址: 程序人生 >> STL lexicographical_compare
作者:代码疯子(Wins0n) 本站内容如无声明均属原创,转载请保留作者信息与原文链接,谢谢!


更多



  1. 本文目前尚无任何评论.
  1. 本文目前尚无任何 trackbacks 和 pingbacks.