c++ STL find search
#include <iostream>
#include <algorithm>
#include <deque>
#include <list>
#include <vector>
#include <functional>
using namespace std;
//二元谓词
bool isEven(int elementParam, bool even)
{
if (even)
{
return elementParam % 2 == 0;
}
else
{
return elementParam % 2 == 0;
}
}
int main()
{
deque<int> deque1;
list<int> list1;
for (int k=0;k<10;k++)
{
deque1.insert(deque1.end(),k);
}
for (int k = 1; k<11; k++)
{
deque1.insert(deque1.end(), k);
}
for (int k = 3; k<10; k++)
{
deque1.insert(deque1.end(), k);
}
deque<int>::iterator deque_iter;
for (deque_iter = deque1.begin();deque_iter != deque1.end();++deque_iter)
{
cout << *deque_iter << " ";
}
cout << endl;
for (int k=4;k<10;k++)
{
list1.push_back(k);
}
list<int>::iterator list_iter1;
for (list_iter1 = list1.begin();list_iter1 != list1.end();++list_iter1)
{
cout << *list_iter1 << " ";
}
cout << endl;
deque<int>::iterator deque_iter1;
deque_iter1 = search(deque1.begin(), deque1.end(), list1.begin(), list1.end());
while (deque_iter1 != deque1.end())
{
cout << "find ! element position:" <<distance(deque1.begin(),deque_iter1)+1<< endl;
++deque_iter1;
deque_iter1 = search(deque_iter1, deque1.end(), list1.begin(), list1.end());
}
cout << "------------------------------------------------" << endl;
vector<int> vector1;
for (int k=0;k<10;k++)
{
vector1.push_back(k);
}
vector1.push_back(10);
vector1.push_back(12);
vector1.push_back(14);
vector<int>::iterator vector_iter1;
for (vector_iter1 = vector1.begin();vector_iter1 != vector1.end();++vector_iter1)
{
cout << *vector_iter1 << " ";
}
cout << endl;
bool checkEventArgs[3] = { true,false,true };
vector<int>::iterator vector_iter2;
vector_iter2 = search(vector1.begin(), vector1.end(),checkEventArgs,checkEventArgs+3, isEven);
if (vector_iter2 != vector1.end())
{
cout << "find:" << *vector_iter2 << endl;
}
else
{
cout << "not find.." << endl;
}
system("pause");
return 0;
}
=====================================================
0 1 2 3 4 5 6 7 8 9 1 2 3 4 5 6 7 8 9 10 3 4 5 6 7 8 9
4 5 6 7 8 9
find ! element position:5
find ! element position:14
find ! element position:22
------------------------------------------------
0 1 2 3 4 5 6 7 8 9 10 12 14
find:10
请按任意键继续. . .