博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
STL_算法_区间的比較(equal、mismatch、 lexicographical_compare)
阅读量:7229 次
发布时间:2019-06-29

本文共 4352 字,大约阅读时间需要 14 分钟。

C++ Primer 学习中。。

 

简单记录下我的学习过程 (代码为主)

//全部容器适用

equal(b,e,b2)       //用来比較第一个容器[b,e)和第二个容器b2开头,是否相等
equal(b,e,b2,p)

mismatch(b,e,b2)    //用来查找两个容器中第一个不相等的数据,返回迭代器
mismatch(b,e,b2,p)

lexicographical_compare(b,e,b2,e2)      //用来比較第一个区间是否比第二个区间小
lexicographical_compare(b,e,b2,e2,p)

#include
#include
#include
#include
#include
#include
using namespace std;/*****************************************//全部容器适用equal(b,e,b2) //用来比較第一个容器[b,e)和第二个容器b2开头。是否相等equal(b,e,b2,p)mismatch(b,e,b2) //用来查找两个容器中第一个不相等的数据,返回迭代器mismatch(b,e,b2,p)lexicographical_compare(b,e,b2,e2) //用来比較第一个区间是否比第二个区间小lexicographical_compare(b,e,b2,e2,p)*****************************************//*************************************************************************************std::equal 全部排序容器适用 algorithm--------------------------------------------------------------------------------------template
bool equal ( InputIterator1 first1, InputIterator1 last1, InputIterator2 first2 );template
bool equal ( InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, BinaryPredicate pred );//eg:template
bool equal ( InputIterator1 first1, InputIterator1 last1, InputIterator2 first2 ){ while ( first1!=last1 ) { if (!(*first1 == *first2)) // or: if (!pred(*first1,*first2)), for pred version return false; ++first1; ++first2; } return true;}*************************************************************************************//*************************************************************************************std::mismatch 全部排序容器适用 algorithm--------------------------------------------------------------------------------------template
pair
mismatch (InputIterator1 first1, InputIterator1 last1, InputIterator2 first2 );template
pair
mismatch (InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, BinaryPredicate pred );//eg:template
pair
mismatch (InputIterator1 first1, InputIterator1 last1, InputIterator2 first2 ){ while ( (first1!=last1) && (*first1==*first2) ) // or: pred(*first1,*first2), for the pred version { ++first1; ++first2; } return make_pair(first1,first2);}*************************************************************************************//*************************************************************************************std::lexicographical_compare 全部排序容器适用 algorithm--------------------------------------------------------------------------------------template
bool lexicographical_compare ( InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, InputIterator2 last2 );template
bool lexicographical_compare ( InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, InputIterator2 last2, Compare comp );//eg:template
bool lexicographical_compare ( InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, InputIterator2 last2 ){ while (first1!=last1) { if (first2==last2 || *first2<*first1) return false; else if (*first1<*first2) return true; first1++; first2++; } return (first2!=last2);}*************************************************************************************/bool mypredicate (int i, int j){ return (i==j);}bool mycomp (char c1, char c2){ return tolower(c1)
myvector (myints,myints+5); // myvector: 20 40 60 80 100 // using default comparison: if (equal (myvector.begin(), myvector.end(), myints)) cout << "The contents of both sequences are equal." << endl; else cout << "The contents of both sequences differ." << endl; myvector[3]=81; // myvector: 20 40 60 81 100 // using predicate comparison: if (equal (myvector.begin(), myvector.end(), myints, mypredicate)) cout << "The contents of both sequences are equal." << endl; else cout << "The contents of both sequences differ." << endl; cout<
mylist; for (int i=1; i<6; i++) mylist.push_back (i*10); // mylist: 10 20 30 40 50 int myints2[] = {10,20,80,40,1024}; // myints2: 10 20 80 40 1024 pair
::iterator,int*> mypair; // using default comparison: mypair = mismatch (mylist.begin(), mylist.end(), myints2); cout << "First mismatching elements: " << *mypair.first; cout << " and " << *mypair.second << endl; mypair.first++; mypair.second++; // using predicate comparison: mypair = mismatch (mypair.first, mylist.end(), mypair.second, mypredicate); cout << "Second mismatching elements: " << *mypair.first; cout << " and " << *mypair.second << endl; cout <

转载地址:http://ondfm.baihongyu.com/

你可能感兴趣的文章
5月末周中国.COM总量净增1.2万个 美国净减2.6万个
查看>>
Elasticsearch数据建模-关联查询
查看>>
我的友情链接
查看>>
CentOS 下安装 Lnmp
查看>>
redis系列:通过日志案例学习string命令
查看>>
世界冠军之路:菜鸟车辆路径规划求解引擎研发历程
查看>>
Linux-sendmail
查看>>
关于BSTR的困惑
查看>>
什么时候使用HashMap?它有什么特点?
查看>>
框架名
查看>>
编译安装PHP
查看>>
插入透明背景Flash的HTML代码
查看>>
无标题
查看>>
我的友情链接
查看>>
Web前端入门学习(3)——CSS选择器
查看>>
DNS的搭建
查看>>
Apache/Nginx 访问日志分析脚本
查看>>
Curator的使用
查看>>
第五章 集合类型
查看>>
我的友情链接
查看>>