Or login with:
#include <algorithm> template < class ForwardIterator1, class ForwardIterator2 > ForwardIterator1 search( ForwardIterator1 first1, ForwardIterator1 last1, ForwardIterator2 first2, ForwardIterator2 last2 ); template < class ForwardIterator1, class ForwardIterator2, class Pr > ForwardIterator1 search( ForwardIterator1 first1, ForwardIterator1 last1, ForwardIterator2 first2, ForwardIterator2 last2 BinaryPredicate comp );Parameters:
| Parameter | Description |
|---|---|
| first1 | A forward iterator addressing the position of the first element in the range to be searched |
| last1 | A forward iterator addressing the position one past the final element in the range to be searched |
| first2 | A forward iterator addressing the position of the first element in the range to be matched |
| last2 | A forward iterator addressing the position one past the final element in the range to be matched |
| comp | User-defined predicate function object that defines the condition to be satisfied if two elements are to be taken as equivalent. A binary predicate takes two arguments and returns true when satisfied and false when not satisfied |
first2 to last2 ) into sequence (elements from first1 to last1 ).
The two forms of search differ in how they determine if two elements are the same: the first uses operator== and the second uses the user-supplied function object comp.last1 is returned.#include <iostream> #include <vector> #include <algorithm> using namespace std; int main() { int a1[] = {1, 2, 3, 4, 5, 6, 7, 8, 3, 4, 5, 9, 10, 11}; vector<int> v1(a1, a1+14); cout <<"\nHere are the contents of v1:\n"; for (vector<int>::size_type i=0; i<v1.size(); i++) cout <<v1.at(i)<<" "; int a2[] = {3, 4, 5}; vector<int> v2(a2, a2+3); cout <<"\nHere are the contents of v2:\n"; for (vector<int>::size_type i=0; i<v2.size(); i++) cout <<v2.at(i)<<" "; vector<int>::iterator p; p = search(v1.begin(), v1.end(), v2.begin(), v2.end()); if (p != v1.end()) cout <<"\nThe first instance of v2 in v1 begins at location "<<(int)(p-v1.begin()+1)<<"."; else cout <<"\nNo instance of v2 was found in v1."; p = search(p+1, v1.end(), v2.begin(), v2.end()); if (p != v1.end()) cout <<"\nThe next instance of v2 in v1 begins at location "<<(int)(p-v1.begin()+1)<<"."; else cout<<"\nNo further instance of v2 was found in v1."; p = search(p+1, v1.end(), v2.begin(), v2.end()); if (p != v1.end()) cout <<"\nThe next instance of v2 in v1 begins at location "<< (int)(p-v1.begin()+1)<<"."; else cout <<"\nNo further instance of v2 was found in v1."; return 0; }
You must login to leave a messge