STL 查找与替换

来源:互联网 发布:fourloko美国被禁 知乎 编辑:程序博客网 时间:2024/05/22 12:57
//SearchReplace.cpp#include <algorithm>#include <functional>#include <vector>#include "PrintSequence.h"using namespace std;struct PlusOne{bool operator()(int i, int j){return j == i;}};class MulMoreThan{int value;public:MulMoreThan(int val) : value(val) {}bool operator()(int v, int m){return v * m > value;}};int main(){int a[] = {1, 2, 3, 4, 5, 6, 6, 7, 7, 7, 8, 8, 8, 8, 11, 11, 11, 11, 11};const int ASZ = sizeof a / sizeof *a;vector<int> v(a, a + ASZ);print(v.begin(), v.end(), "v", " ");vector<int>::iterator it = find(v.begin(), v.end(), 4); //查找4是否在序列中出现,如果出现返回4在该序列第一次出现的位置,否则返回v.end()cout << "find: " << *it << endl;it = find_if(v.begin(), v.end(), bind2nd(greater<int>(), 8)); //返回满足该条件的第一个元素的位置cout << "find_if: " << *it << endl;it = adjacent_find(v.begin(), v.end());//返回两个相邻且相等元素的第一个元素的位置while(it != v.end()){cout << "adjacent_find: " << *it << ", " << *(it + 1) << endl;it = adjacent_find(it + 1, v.end());}it = adjacent_find(v.begin(), v.end(), PlusOne());//返回满足该条件的第一个元素的位置while(it != v.end()){cout << "adjacent_find PlusOne: " << *it << ", " << *(it + 1) << endl;it = adjacent_find(it + 1, v.end(), PlusOne());}int b[] = {8, 11};const int BSZ = sizeof b / sizeof *b;print(b, b + BSZ, "b", " ");it = find_first_of(v.begin(), v.end(), b, b + BSZ);//在第二个范围内查找与第一个范围内某元素相等的元素,返回相等元素在第一个范围内第一次出现的位置print(it, it + BSZ, "find_first_of", " ");it = find_first_of(v.begin(), v.end(), b, b + BSZ, PlusOne());           //查找使得条件满足的元素print(it, it + BSZ, "find_first_of PlusOne", " ");it = search(v.begin(), v.end(), b, b + BSZ);print(it, it + BSZ, "search", " ");//检查第二个序列范围的元素是否出现在第一个序列范围内(顺序也一致),如果是则返回第二个序列出现的第一个序列的最开始的位置int c[] = {6, 6, 7};const int CSZ = sizeof c / sizeof *c;print(c, c + CSZ, "c", " ");it = search(v.begin(), v.end(), c, c + CSZ, PlusOne());//检查被比较的元素对是否能让PlusOne()返回trueif(it != v.end())print(it, it + CSZ, "search PlusOne", " ");else cout << "Have not find it!" << endl;int d[] = {11, 11, 11};const int DSZ = sizeof d / sizeof *d;print(d, d + DSZ, "d", " ");it = find_end(v.begin(), v.end(), d, d + DSZ);//逆序检查第二个序列范围的元素是否出现在第一个序列范围内(顺序也一致),如果是则返回第二个序列出现的第一个序列的最开始的位置print(it, it + DSZ, "find_end", " ");int e[] = {8, 8};const int ESZ = sizeof e / sizeof *e;print(e, e + ESZ, "e", " ");it = find_end(v.begin(), v.end(), e, e + ESZ, PlusOne());print(it, it + ESZ, "find_end PlusOne", " ");//逆序检查被比较的元素对是否能让PlusOne()返回trueit = search_n(v.begin(), v.end(), 3, 7);//在第一序列范围内查找共3个连续的7,返回第一个7的地址print(it, it + 3, "search_n 3,7", " ");it = search_n(v.begin(), v.end(), 6, 15, MulMoreThan(100));print(it, it + 6, "search_n 6, 15. MulMoreThan(100)", " ");cout << "min_element: "<< *min_element(v.begin(), v.end()) << endl;cout << "max_element: "<< *max_element(v.begin(), v.end()) << endl;vector<int> v2;replace_copy(v.begin(), v.end(),back_inserter(v2), 8, 47);print(v2.begin(), v2.end(), "replace_copy 8 -> 47", " ");replace_if(v.begin(), v.end(),bind2nd(greater_equal<int>(), 7), -1);print(v.begin(), v.end(), "replace_if >= 7 -> -1", " ");system("pause");return 0;}/*结果:v:  1 2 3 4 5 6 6 7 7 7 8 8 8 8 11 11 11 11 11find: 4find_if: 11adjacent_find: 6, 6adjacent_find: 7, 7adjacent_find: 7, 7adjacent_find: 8, 8adjacent_find: 8, 8adjacent_find: 8, 8adjacent_find: 11, 11adjacent_find: 11, 11adjacent_find: 11, 11adjacent_find: 11, 11adjacent_find PlusOne: 6, 6adjacent_find PlusOne: 7, 7adjacent_find PlusOne: 7, 7adjacent_find PlusOne: 8, 8adjacent_find PlusOne: 8, 8adjacent_find PlusOne: 8, 8adjacent_find PlusOne: 11, 11adjacent_find PlusOne: 11, 11adjacent_find PlusOne: 11, 11adjacent_find PlusOne: 11, 11b:  8 11find_first_of:  8 8find_first_of PlusOne:  8 8search:  8 11c:  6 6 7search PlusOne:  6 6 7d:  11 11 11find_end:  11 11 11e:  8 8find_end PlusOne:  8 8search_n 3,7:  7 7 7search_n 6, 15. MulMoreThan(100):  8 8 11 11 11 11min_element: 1max_element: 11replace_copy 8 -> 47:  1 2 3 4 5 6 6 7 7 7 47 47 47 47 11 11 11 11 11replace_if >= 7 -> -1:  1 2 3 4 5 6 6 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1*/

原创粉丝点击