区间的比较
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)
:比较第一个迭代器区间[b, e)
是否比第二个[b2, e2)
小lexicographical_compare(b, e, b2, e2, p)
equal()
#include <iostream>
#include <vector>
#include <list>
#include <algorithm>
using namespace std;
bool bothEvenOrOdd(int elem1, int elem2)
{
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.insert(ilist.end(), i);
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;
if (equal(ivec.begin(), ivec.end(), ilist.begin()))
cout << "这两个区间相等" << endl;
else
cout << "这两个区间不相等" << endl;
if (equal(ivec.begin(), ivec.end(), ilist.begin(), bothEvenOrOdd))
cout << "这两个区间是奇数对应奇数,偶数对应偶数" << endl;
else
cout << "这两个区间不是奇数对应奇数,偶数对应偶数" << endl;
cout << endl;
return 0;
}
mismatch()
mismatch()
返回值为pair
,第一个迭代器指向第一个容器里第一个不相等的数,第二个迭代器指向第二个容器里第一个不相等的数。若未找到,则第一个迭代器返回第一个容器里的.end()
。mismatch()
不能用于判断是否相等,因为可能两个区间不等长,第一个区间比较完后第二个区间还有数。
#include <iostream>
#include <vector>
#include <list>
#include <algorithm>
#include <functional>
using namespace std;
bool bothEvenOrOdd(int elem1, int elem2)
{
return elem1 % 2 == elem2 % 2;
}
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;
pair<vector<int>::iterator, list<int>::iterator> values;
values = mismatch(ivec.begin(), ivec.end(), ilist.begin());
if (values.first == ivec.end())
cout << "没有找到不相等的数:no mismatch" << endl;
else
cout << "first mismatch:" << *values.first << " and " << *values.second << endl;
values = mismatch(ivec.begin(), ivec.end(), ilist.begin(), less_equal<int>());
if (values.first == ivec.end())
cout << "没找到!(always less-or-equal)" << endl;
else
cout << "找到了(not less-or-equal):" << *values.first << ", " << *values.second << endl;
cout << endl;
return 0;
}
lexicographi_compare()
- 检查第一个区间小于第二个区间算法:比较第一个区间是否比第二个区间小。
- 区间元素数目多少:数目多则大,数目少则小。
- 数目相同,则判断数字大小
- 返回
true
:第一个区间比第二个区间小;返回false
:两个区间相等。
#include <iostream>
#include <vector>
#include <list>
#include <algorithm>
#include <functional>
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);
printCollection(c1);
printCollection(c2);
printCollection(c3);
printCollection(c4);
if (lexicographical_compare(c4.begin(), c4.end(), c1.begin(), c1.end()))
cout << "c4<c1" << endl;
else
cout << "c4≥c1" << endl;
if (lexicographical_compare(c4.begin(), c4.end(), c3.begin(), c3.end()))
cout << "c4<c3" << endl;
else
cout << "c4≥c3" << endl;
cout << 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);
for_each(cc.begin(), cc.end(), printCollection);
cout << endl;
sort(cc.begin(), cc.end(), lessForCollection);
for_each(cc.begin(), cc.end(), printCollection);
cout << endl;
return 0;
}