`

lexicographical_compare

阅读更多

lexicographical_compare:

  C++ STL 泛型算法函数:用于按字典序比较两个序列。

  函数申明:

  //重载1,如果[first1, last1)按字典序列小于[first2, last2),返回true,否则返回false。

  template <class InputIterator1, class InputIterator2 >

  bool

  lexicographical_compare(

  InputIterator1 first1, InputIterator1 last1,

  InputIterator2 first2, InputIterator2 last2 );

  //重载2,功能同重载1,增加了比较函数comp,即大小关系由comp函数确定。

  template < class InputIterator1, class InputIterator2,

  class Compare >

  bool

  lexicographical_compare(

  InputIterator1 first1, InputIterator1 last1,

  InputIterator2 first2, InputIterator2 last2,

  Compare comp );

  示例代码:

  #include <algorithm>

  #include <iostream>

  #include <iterator>

  using namespace std;

  void Output(const int * a,const int & a_size,const int * b,const int & b_size)

  {

  cout<<"a[]=";

  copy(a,a+a_size,ostream_iterator<int>(cout," "));

  cout<<endl;

  cout<<"b[]=";

  copy(b,b+b_size,ostream_iterator<int>(cout," "));

  cout<<endl;

  }

  typedef bool (* CMP)(const int &,const int &);

  int Compare(const int * a,const int & a_size,const int * b,const int & b_size,CMP cmp) //按照cmp方式进行比较

  {

  bool ASmallerThanB,BSmallerThanA;

  ASmallerThanB=lexicographical_compare(a,a+a_size,b,b+b_size,cmp); //按照cmp方式进行比较

  BSmallerThanA=lexicographical_compare(b,b+b_size,a,a+a_size,cmp);

  if(! ASmallerThanB && ! BSmallerThanA) //a[] 和 b[] 相等

  return 0;

  else if(ASmallerThanB) //a[] < b[]

  return -1;

  else

  return 1;

  }

  int Compare(const int * a,const int & a_size,const int * b,const int & b_size) //默认比较方式

  {

  bool ASmallerThanB,BSmallerThanA;

  ASmallerThanB=lexicographical_compare(a,a+a_size,b,b+b_size); //默认比较方式

  BSmallerThanA=lexicographical_compare(b,b+b_size,a,a+a_size);

  if(! ASmallerThanB && ! BSmallerThanA) //a[] 和 b[] 相等

  return 0;

  else if(ASmallerThanB) //a[] < b[]

  return -1;

  else

  return 1;

  }

  bool greater(const int & a,const int & b) //自定义越大的字典序越小

  {

  return a>b;

  }

  int main()

  {

  int a[]={1,2,3,4,5,6};

  int b[]={1,2,3,4,5,6};

  Output(a,6,b,6);

  cout<<"Compare(a,6,b,6)="<<Compare(a,6,b,6)<<endl;

  cout<<"Compare(a,6,b,6,greater)="<<Compare(a,6,b,6,greater)<<endl;

  rotate(a+2,a+4,a+6);

  Output(a,6,b,6);

  cout<<"Compare(a,6,b,6)="<<Compare(a,6,b,6)<<endl;

  cout<<"Compare(a,6,b,6,greater)="<<Compare(a,6,b,6,greater)<<endl;

  return 0;

  }

  return a>b;

  }

  int main()

  {

  int a[]={1,2,3,4,5,6};

  int b[]={1,2,3,4,5,6};

  Output(a,6,b,6);

  cout<<"Compare(a,6,b,6)="<<Compare(a,6,b,6)<<endl;

  cout<<"Compare(a,6,b,6,greater)="<<Compare(a,6,b,6,greater)<<endl;

  rotate(a+2,a+4,a+6);

  Output(a,6,b,6);

  cout<<"Compare(a,6,b,6)="<<Compare(a,6,b,6)<<endl;

  cout<<"Compare(a,6,b,6,greater)="<<Compare(a,6,b,6,greater)<<endl;

  return 0;

  }

分享到:
评论

相关推荐

    ntl:用于 Windows NT 内核模式驱动程序的 STL 的非常微小和选择性的实现

    lexicographical_compare ;班级助手类integral_constant ; bool_constant ; true_type ; false_type ;主要类型类别is_class (因为is_union没有实现,它不检测联合); is_pointer ; is_lvalue_reference ( _v );...

    effective stl 中文 pdf

    条款35: 通过mismatch或lexicographical_compare实现简单的忽略大小写字符串比较 条款36: 用not1和remove_copy_if来表现copy_if 条款37: 用accumulate或for_each来统计序列 仿函数,仿函数类,函数等等 条款38: ...

    Effictive STL CHM中文版

    条款35: 通过mismatch或lexicographical_compare实现简单的忽略大小写字符串比较 条款36: 用not1和remove_copy_if来表现copy_if 条款37: 用accumulate或for_each来统计序列 仿函数,仿函数类,函数等等 条款38: ...

    C++ STL 开发技术导引(第6章)

    23.27 字典比较lexicographical_compare 405 23.28 下一排列组合next_permutation 406 23.29 上一排列组合prev_permutation 409 23.30 本章小结 411 第24章 数值算法 412 24.1 递增赋值iota 412 24.2 ...

    C++ STL开发技术导引(第5章)

    23.27 字典比较lexicographical_compare 405 23.28 下一排列组合next_permutation 406 23.29 上一排列组合prev_permutation 409 23.30 本章小结 411 第24章 数值算法 412 24.1 递增赋值iota 412 24.2 ...

    C++ STL开发技术导引(第3章)

    23.27 字典比较lexicographical_compare 405 23.28 下一排列组合next_permutation 406 23.29 上一排列组合prev_permutation 409 23.30 本章小结 411 第24章 数值算法 412 24.1 递增赋值iota 412 24.2 ...

    -C++参考大全(第四版) (2010 年度畅销榜

    34.19 lexicographical_compare 34.20 lower_bound 34.21 make_heap 34.22 max 34.23 max_element 34.24 merge 34.25 min 34.26 min_element 34.27 mismatch 34.28 next_permutation 34.29 nth_element 34.30 ...

    STL源码剖析.pdg

    lexicographical_compare 310 max, min 312 mismatch 313 swap 314 6.4.3 copy,强化效率无所不用其极 314 6.4.4 copy_backward 326 6.5 set 相关算法(应用于有序区间) 328 6.5.1 set_union 331 6.5.2 set...

    STL 源码剖析(侯捷先生译著)

    lexicographical_compare 310 max, min 312 mismatch 313 swap 314 6.4.3 copy,强化效率无所不用其极 314 6.4.4 copy_backward 326 6.5 Set 相关算法(应用于有序区间) 328 6.5.1 set_union 331 6.5.2 set...

    js-compare:JavaScript的比较功能

    import { reverse , lexicographical , increasing } from '@aureooms/js-compare' ;let compare = reverse ( lexicographical ( increasing ) ) ;compare ( [ 1 , 1 ] , [ 1 , 2 ] ) &gt; 0 ; // true :scroll: 参考

    SDL_name.rar_Code Name

    For copy n and lexicographical compare 3way Source Code for Embedded Linux.

    LeetCode最全代码

    ...The number of questions is increasing recently. Here is the classification of all `468` questions. ...I'll keep updating for full summary and better solutions....|-----|---------------- | --------------- |...

    A Durable Main-Memory Index Using Flash-计算机科学

    • Insert, find, delete, compare & swap,and iterate (lexicographical ordered traversal)• Should support high-concurrent accesses • All single-key operations must be atomic• SSD is

    kgb档案压缩console版+源码

    is restricted to an increasingly narrow lexicographical range containing y. All of the strings in this range will share a growing prefix. Each time the prefix grows, we can output a character. y +-...

Global site tag (gtag.js) - Google Analytics