C++ STL algorithm, another search(), binary predicate and iterator program example
Compiler: Visual C++ Express Edition 2005
Compiled on Platform: Windows XP Pro SP2
Header file: Standard
Additional project setting: Set project to be compiled as C++
Project -> your_project_name Properties -> Configuration Properties -> C/C++ -> Advanced -> Compiled As: Compiled as C++ Code (/TP)
Other info: none
To do: Using the C++ search() to find for the first occurrence of a sequence within a target range whose elements are equal to those in a given sequence of elements or whose elements are equivalent in a sense specified by a binary predicate to the elements in the given sequence in C++ programming
To show: How to use the C++ algorithm, search() to search for the first occurrence of a sequence within a target range whose elements are equal to those in a given sequence of elements or whose elements are equivalent in a sense specified by a binary predicate to the elements in the given sequence in C++ programming
// C++ STL algorithm, search()
#include <vector>
#include <list>
#include <algorithm>
#include <iostream>
using namespace std;
// return whether second element is twice the first
bool twice(int elem1, int elem2)
{return (2 * elem1 == elem2);}
int main(void)
{
// vector container
vector <int> vec1, vec2;
// list container
list <int> lst1;
// iterator for vector
vector <int>::iterator Iter1, Iter2;
// iterator for list
list <int>::iterator lst1_Iter, lst1_inIter;
int i, j, k;
// pushing data into the vector and list
for(i = 0; i <= 5; i++)
vec1.push_back(5*i);
for(i = 0; i <= 5; i++)
vec1.push_back(5*i);
for(j = 4; j <= 5; j++)
lst1.push_back(5*j);
for(k = 2; k <= 4; k++)
vec2.push_back(10*k);
// print the data
cout<<"vec1 vector data: ";
for(Iter1 = vec1.begin(); Iter1 != vec1.end(); Iter1++)
cout<<*Iter1<<" ";
cout<<endl;
cout<<"\nlst1 list data: ";
for(lst1_Iter = lst1.begin(); lst1_Iter!= lst1.end(); lst1_Iter++)
cout<<*lst1_Iter<<" ";
cout<<endl;
cout<<"\nvec2 vector data: ";
for(Iter2 = vec2.begin(); Iter2 != vec2.end(); Iter2++)
cout<<*Iter2<<" ";
cout<<endl<<endl;
// searching vec1 for first match to lst1 under identity
vector <int>::iterator result1;
result1 = search (vec1.begin(), vec1.end(), lst1.begin(), lst1.end());
if(result1 == vec1.end())
cout<<"\nThere is no match of lst1 list in vec1 vector."<<endl;
else
cout<<"There is at least one match of lst1 list in vec1 vector and the first one begins at position "<<int(result1 - vec1.begin())<<endl;
// searching vec1 for a match to lst1 under the binary predicate twice
vector <int>::iterator result2;
result2 = search(vec1.begin(), vec1.end(), vec2.begin(), vec2.end(), twice);
if(result2 == vec1.end())
cout<<"\nThere is no match of lst1 list in vec1 vector."<<endl;
else
cout<<"\nThere is a sequence of elements in vec1 vector that are equivalent to those in vec2 vector\nunder the binary "
<<"predicate twice and the first one begins at position "
<<int(result2 - vec1.begin())<<endl;
return 0;
}
Output examples:
vec1 vector data: 0 5 10 15 20 25 0 5 10 15 20 25
lst1 list data: 20 25
vec2 vector data: 20 30 40
There is at least one match of lst1 list in vec1 vector and the first one begins at position 4
There is a sequence of elements in vec1 vector that are equivalent to those in vec2 vector
under the binary predicate twice and the first one begins at position 2
Press any key to continue . . .