// equal algorithm example#include<iostream>// std::cout#include<algorithm>// std::equal#include<vector>// std::vectorbool mypredicate (int i,int j){return(i==j);}int main (){int myints[]={20,40,60,80,100};// myints: 20 40 60 80 100
std::vector<int>myvector (myints,myints+5);// myvector: 20 40 60 80 100// using default comparison:if( std::equal (myvector.begin(), myvector.end(), myints))
std::cout <<"The contents of both sequences are equal.\n";else
std::cout <<"The contents of both sequences differ.\n";
myvector[3]=81;// myvector: 20 40 60 81 100// using predicate comparison:if( std::equal (myvector.begin(), myvector.end(), myints, mypredicate))
std::cout <<"The contents of both sequences are equal.\n";else
std::cout <<"The contents of both sequences differ.\n";return0;}
Output:
The contents of both sequences are equal.
The contents of both sequence differ.
// find example#include<iostream>// std::cout#include<algorithm>// std::find#include<vector>// std::vectorint main (){// using std::find with array and pointer:int myints[]={10,20,30,40};int* p;
p = std::find (myints, myints+4,30);if(p != myints+4)
std::cout <<"Element found in myints: "<<*p <<'\n';else
std::cout <<"Element not found in myints\n";// using std::find with vector and iterator:
std::vector<int> myvector (myints,myints+4);
std::vector<int>::iterator it;
it = find (myvector.begin(), myvector.end(),30);if(it != myvector.end())
std::cout <<"Element found in myvector: "<<*it <<'\n';else
std::cout <<"Element not found in myvector\n";return0;}
Output:
Element found in myints:30
Element found in myvector:30
// find_end example#include<iostream>// std::cout#include<algorithm>// std::find_end#include<vector>// std::vectorbool myfunction (int i,int j){return(i==j);}int main (){int myints[]={1,2,3,4,5,1,2,3,4,5};
std::vector<int> haystack (myints,myints+10);int needle1[]={1,2,3};// using default comparison:
std::vector<int>::iterator it;
it = std::find_end (haystack.begin(), haystack.end(), needle1, needle1+3);if(it!=haystack.end())
std::cout <<"needle1 last found at position "<<(it-haystack.begin())<<'\n';int needle2[]={4,5,1};// using predicate comparison:
it = std::find_end (haystack.begin(), haystack.end(), needle2, needle2+3, myfunction);if(it!=haystack.end())
std::cout <<"needle2 last found at position "<<(it-haystack.begin())<<'\n';return0;}
Output:
needle1 found at position 5
needle2 found at position 3
// find_first_of example#include<iostream>// std::cout#include<algorithm>// std::find_first_of#include<vector>// std::vector#include<cctype>// std::tolowerbool comp_case_insensitive (char c1,char c2){return(std::tolower(c1)==std::tolower(c2));}int main (){int mychars[]={'a','b','c','A','B','C'};
std::vector<char> haystack (mychars,mychars+6);
std::vector<char>::iterator it;int needle[]={'A','B','C'};// using default comparison:
it = find_first_of (haystack.begin(), haystack.end(), needle, needle+3);if(it!=haystack.end())
std::cout <<"The first match is: "<<*it <<'\n';// using predicate comparison:
it = find_first_of (haystack.begin(), haystack.end(),
needle, needle+3, comp_case_insensitive);if(it!=haystack.end())
std::cout <<"The first match is: "<<*it <<'\n';return0;}
Output:
The first match is: A
The first match is: a