*cb36a_c++_STL_算法_区间的比较equal_mismatch_lexicographical_compare
区间:容器中的全部数据或者部分数据,都叫做区间
equal(b,e,b2),比较两个容器数据是不是相等 ,b(容器1,迭代器begin()),e(容器1,迭代器end(),b2(容器2,迭代器2指向的位置,begin2
if(equal(ivec.begin(),ivec.end(),ilist.begin()))
equal(b,e,b2,p) p,parameter,函数对象,谓词
mismatch(b,e,b2)比较两个容器中第一个不相等的数据
mismatch(b,e,b2,p)
lexicographical_compare(b,e,b2,e2)用来比较第一个区间是不是不第二个区间小,区间1<区间2
lexicographical_compare(b,e,b2,e2,p)
两个字符串的字母排序是通过从第一个字符开始比较对应字符得到的。第一对不同的对应字符决定了哪个字符串排在首位。
字符串的顺序就是不同字符的顺序。如果字符串的长度相同,而且所有的字符都相等,那么这些字符串就相等。
如果字符串的长度不同,短字符串的字符序列和长字符串的初始序列是相同的,那么短字符串小于长字符串。因此 “age” 在“beauty” 之前,
“a lull” 在 “a storm” 之前。显然,“the chicken” 而不是 “the egg” 会排在首位。
对于任何类型的对象序列来说,字典序都是字母排序思想的泛化。
从两个序列的第一个元素开始依次比较对应的元素,前两个对象的不同会决定序列的顺序。显然,序列中的对象必须是可比较的。
lexicographical_compare()算法可以比较由开始和结束迭代器定义的两个序列。
它的前两个参数定义了第一个序列,第 3 和第 4 个参数分别是第二个序列的开始和结束迭代器。
默认用 < 运算符来比较元素,但在需要时,也可以提供一个实现小于比较的函数对象作为可选的第 5 个参数。
如果第一个序列的字典序小于第二个,这个算法会返回 true,否则返回 false。所以,返回 false 表明第一个序列大于或等于第二个序列。
/*cb36a_c++_STL_算法_区间的比较equal_mismatch_lexicographical_compare
区间:容器中的全部数据或者部分数据,都叫做区间
equal(b,e,b2),比较两个容器数据是不是相等 ,b(容器1,迭代器begin()),e(容器1,迭代器end(),b2(容器2,迭代器2指向的位置,begin2
if(equal(ivec.begin(),ivec.end(),ilist.begin()))
equal(b,e,b2,p) p,parameter,函数对象,谓词
mismatch(b,e,b2)比较两个容器中第一个不相等的数据
mismatch(b,e,b2,p)
lexicographical_compare(b,e,b2,e2)用来比较第一个区间是不是不第二个区间小,区间1<区间2
lexicographical_compare(b,e,b2,e2,p)
两个字符串的字母排序是通过从第一个字符开始比较对应字符得到的。第一对不同的对应字符决定了哪个字符串排在首位。
字符串的顺序就是不同字符的顺序。如果字符串的长度相同,而且所有的字符都相等,那么这些字符串就相等。
如果字符串的长度不同,短字符串的字符序列和长字符串的初始序列是相同的,那么短字符串小于长字符串。因此 “age” 在“beauty” 之前,
“a lull” 在 “a storm” 之前。显然,“the chicken” 而不是 “the egg” 会排在首位。
对于任何类型的对象序列来说,字典序都是字母排序思想的泛化。
从两个序列的第一个元素开始依次比较对应的元素,前两个对象的不同会决定序列的顺序。显然,序列中的对象必须是可比较的。
lexicographical_compare()算法可以比较由开始和结束迭代器定义的两个序列。
它的前两个参数定义了第一个序列,第 3 和第 4 个参数分别是第二个序列的开始和结束迭代器。
默认用 < 运算符来比较元素,但在需要时,也可以提供一个实现小于比较的函数对象作为可选的第 5 个参数。
如果第一个序列的字典序小于第二个,这个算法会返回 true,否则返回 false。所以,返回 false 表明第一个序列大于或等于第二个序列。
*/
#include <iostream>
#include <algorithm>
#include <vector>
#include <list>
using namespace std;
bool bothEvenOrOdd(int elem1, int elem2)//二元谓词,返回是bool型,就是谓词
{
return elem1%2 == elem2%2;
}
//两个容器奇数偶数对应。
int main()
{
vector<int> ivec;
list<int> ilist;
for (int i = 1; i <= 7; ++i)
ivec.push_back(i);
for (int i = 3; i <= 9; ++i)
ilist.push_back(i);
cout << "vector里面的数据:" << endl;
for (vector<int>::iterator iter = ivec.begin(); iter != ivec.end(); ++iter)
cout<< *iter << ' ';
cout << endl;
cout << "list里面的数据:" << endl;
for (list<int>::iterator iter = ilist.begin(); iter != ilist.end(); ++iter)
cout << *iter << ' ';
cout << endl;
if (equal(ivec.begin(), ivec.end(), ilist.begin()))
cout << "ivec等于ilsit" << endl;
else
cout << "ivec不等于ilist" << endl;
//谓词比较
//equal(b,e,b2,p) p,parameter,函数对象,谓词
两个容器奇数偶数对应。
if (equal(ivec.begin(), ivec.end(), ilist.begin(), bothEvenOrOdd))
{
cout << "两个容器奇数偶数对应" << endl;
}
else
cout << "两个容器奇数偶数不对应" << endl;
return 0;
}
/*
//mismatch 返回值是pair,就是返回1对迭代器
pair<vector<int>::iterator, list<int>::iterator> values;
values = mismatch(ivec.begin(), ivec.end(), ilist.begin());
cout << "找到了。/ivec里面的第一个小于等于list里面的数: " <<values.first<<" and 容器2的值:"<<values.second<< endl;
1>d:\users\txwtech\projects\cb36b\cb36b\cb36b.cpp(39): error C2679: 二进制“<<”: 没有找到接受“_Ty1”类型的右操作数的运算符(或没有可接受的转换)
values.first需要解引用。
改为:cout<<*values.first<<endl;
*/
#include <iostream>
#include <algorithm>
#include <vector>
#include <list>
using namespace std;
int main()
{
vector<int> ivec;
list<int> ilist;
for (int i = 1; i <= 6; ++i)
ivec.push_back(i);
for (int i = 1; i <= 16; i *= 2)
ilist.push_back(i);
ilist.push_back(3);
for (vector<int>::iterator iter = ivec.begin(); iter != ivec.end(); ++iter)
cout << *iter << ' ';
cout << endl;
for (list<int>::iterator iter = ilist.begin(); iter != ilist.end(); ++iter)
cout << *iter << ' ';
cout << endl;
//mismatch 返回值是pair,就是返回1对迭代器
pair<vector<int>::iterator, list<int>::iterator> values;
values = mismatch(ivec.begin(), ivec.end(), ilist.begin());
if (values.first == ivec.end())
cout << "没有找到不相等的数,但不代表两个区间相等" << endl;
else
cout << "找到了第一个不相等的,不匹配的数" <<*values.first<<" and "<<*values.second<< endl;
values = mismatch(ivec.begin(), ivec.end(), ilist.begin(), less_equal<int>());
//less_equal<int>(),小于等于。是一个预定义函数对象.作为谓词
//参考:https://www.cnblogs.com/txwtech/p/12328141.html
//ivec里面的第一个小于等于list里面的数
if (values.first == ivec.end())
cout << "没有找到。/ivec里面的第一个小于等于list里面的数" << endl;
else
cout << "找到了。/ivec里面的第一个小于等于list里面的数: " <<*values.first<<" and 容器2的值:"<<*values.second<< endl;
return 0;
}
/*cb36c
lexicographical_compare(b,e,b2,e2)用来比较第一个区间是不是不第二个区间小,区间1<区间2
if (lexicographical_compare(c4.begin(), c4.end(), c1.begin(), c1.end()))
lexicographical_compare(b,e,b2,e2,p)
两个字符串的字母排序是通过从第一个字符开始比较对应字符得到的。第一对不同的对应字符决定了哪个字符串排在首位。
字符串的顺序就是不同字符的顺序。如果字符串的长度相同,而且所有的字符都相等,那么这些字符串就相等。
如果字符串的长度不同,短字符串的字符序列和长字符串的初始序列是相同的,那么短字符串小于长字符串。因此 “age” 在“beauty” 之前,
“a lull” 在 “a storm” 之前。显然,“the chicken” 而不是 “the egg” 会排在首位。
对于任何类型的对象序列来说,字典序都是字母排序思想的泛化。
从两个序列的第一个元素开始依次比较对应的元素,前两个对象的不同会决定序列的顺序。显然,序列中的对象必须是可比较的。
lexicographical_compare()算法可以比较由开始和结束迭代器定义的两个序列。
它的前两个参数定义了第一个序列,第 3 和第 4 个参数分别是第二个序列的开始和结束迭代器。
默认用 < 运算符来比较元素,但在需要时,也可以提供一个实现小于比较的函数对象作为可选的第 5 个参数。
如果第一个序列的字典序小于第二个,这个算法会返回 true,否则返回 false。所以,返回 false 表明第一个序列大于或等于第二个序列。
cout << "8个list排序后,全部打印出来" << endl;
for_each(cc.begin(), cc.end(), printCollection);
cout << endl;
*/
#include <iostream>
#include <algorithm>
#include <list>
#include <vector>
using namespace std;
void printCollection(const list<int>& l)
{
for (list<int>::const_iterator iter = l.begin(); iter != l.end(); ++iter)
{
cout << *iter << ' ';
}
cout << endl;
}
bool lessForCollection(const list<int>& list1,const list<int>& list2)
{
return lexicographical_compare(list1.begin(), list1.end(), list2.begin(), list2.end());
}
int main()
{
list<int> c1, c2, c3, c4;
for (int i = 1; i <= 5; ++i)
c1.push_back(i);
c4 = c3 = c2 = c1;
c1.push_back(7);
c3.push_back(2);
c3.push_back(0);
c4.push_back(2);
cout << "c1: ";
printCollection(c1);
cout << "c2: ";
printCollection(c2);
cout << "c3: ";
printCollection(c3);
cout << "c4: ";
printCollection(c4);
cout << "lexicogrphical返回布尔bool" << endl;
if (lexicographical_compare(c4.begin(), c4.end(), c1.begin(), c1.end()))
cout << "c4小于c1" << endl;
else
cout << "c4不小于c1" << endl;
if (lexicographical_compare(c2.begin(), c2.end(), c3.begin(), c3.end()))
cout << "c2小于c3" << endl;
else
cout << "c2不小于c3" << endl;
if (lexicographical_compare(c4.begin(), c4.end(), c3.begin(), c3.end()))
cout << "c4小于c3" << endl;
else
cout << "c4不小于c3" << endl;
vector<list<int>> cc;
cc.push_back(c1);
cc.push_back(c2);
cc.push_back(c3);
cc.push_back(c4);
cc.push_back(c3);
cc.push_back(c1);
cc.push_back(c4);
cc.push_back(c2);
cout << "8个list全部打印出来" << endl;
for_each(cc.begin(), cc.end(), printCollection);
cout << endl;
sort(cc.begin(), cc.end(), lessForCollection);
cout << "8个list排序后,全部打印出来" << endl;
for_each(cc.begin(), cc.end(), printCollection);
cout << endl;
return 0;
}