//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()返回true
if(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()返回true
it = 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 11
find: 4
find_if: 11
adjacent_find: 6, 6
adjacent_find: 7, 7
adjacent_find: 7, 7
adjacent_find: 8, 8
adjacent_find: 8, 8
adjacent_find: 8, 8
adjacent_find: 11, 11
adjacent_find: 11, 11
adjacent_find: 11, 11
adjacent_find: 11, 11
adjacent_find PlusOne: 6, 6
adjacent_find PlusOne: 7, 7
adjacent_find PlusOne: 7, 7
adjacent_find PlusOne: 8, 8
adjacent_find PlusOne: 8, 8
adjacent_find PlusOne: 8, 8
adjacent_find PlusOne: 11, 11
adjacent_find PlusOne: 11, 11
adjacent_find PlusOne: 11, 11
adjacent_find PlusOne: 11, 11
b: 8 11
find_first_of: 8 8
find_first_of PlusOne: 8 8
search: 8 11
c: 6 6 7
search PlusOne: 6 6 7
d: 11 11 11
find_end: 11 11 11
e: 8 8
find_end PlusOne: 8 8
search_n 3,7: 7 7 7
search_n 6, 15. MulMoreThan(100): 8 8 11 11 11 11
min_element: 1
max_element: 11
replace_copy 8 -> 47: 1 2 3 4 5 6 6 7 7 7 47 47 47 47 11 11 11 11 11
replace_if >= 7 -> -1: 1 2 3 4 5 6 6 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1
*/
STL 查找与替换
最新推荐文章于 2024-06-19 11:08:23 发布