标签:
STL算法——区间的比较:
equal(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,p)
#include<iostream> #include<algorithm> #include<vector> #include<list> 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.push_back(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; } // system("pause"); return 0; }
#include<iostream> #include<algorithm> #include<vector> #include<list> 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; } // 判断两个区间是否相等一定要用equal /*if (equal(ivec.begin(), ivec.end(), ilist.begin())) cout << "这两个区间相等" << endl; else cout << "这两个区间不相等" << endl; if (equal(ivec.begin(), ivec.end(), ilist.begin(), bothEvenOrOdd)) { cout << "这两个区间是奇数对应奇数,偶数对应偶数" << endl; }*/ // system("pause"); return 0; }第一个数列3 和第二个数列4 ,第一次不匹配
预定义的函数对象
negate<.type>()
plus<type>()
minus<type>()
multiplies<type>()
modulus<type>()
equal_to<type>()
not_equal_to<type>()
less<type>()
greater<type>()
less_equal<type>() // <=
greater_equal<type>()
logical_not<type>()
logical_and<type>()
logical_or<type>()
#include<iostream> #include<algorithm> #include<vector> #include<list> 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>()); values = mismatch(ivec.begin(), ivec.end(), ilist.begin(), islessequal<int, int>); if (values.first == ivec.end()) cout << "没找到!(always less_or_equal)" << endl; else cout << "找到了!(not less_or_equal)" << *values.first << "," << *values.second << endl; // 判断两个区间是否相等一定要用equal /*if (equal(ivec.begin(), ivec.end(), ilist.begin())) cout << "这两个区间相等" << endl; else cout << "这两个区间不相等" << endl; if (equal(ivec.begin(), ivec.end(), ilist.begin(), bothEvenOrOdd)) { cout << "这两个区间是奇数对应奇数,偶数对应偶数" << endl; }*/ // system("pause"); return 0; }
#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); // 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 << 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; // system("pause"); return 0; }
#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(1); 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 << 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; // system("pause"); return 0; }
标签:
原文地址:http://blog.csdn.net/taotaoah/article/details/52216614