Or login with:
#include <algorithm> template < class ForwardIterator1, class ForwardIterator2 > ForwardIterator1 find_first_of( ForwardIterator1 first1, ForwardIterator1 last1, ForwardIterator2 first2, ForwardIterator2 last2 ); template < class ForwardIterator1, class ForwardIterator2, class BinaryPredicate > ForwardIterator1 find_first_of( 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 |
comp.i in [first1, last1) such that, for some iterator j in [first2, last2), *i == *j.
The second returns the first iterator i in [first1, last1) such that, for some iterator j in [first2, last2), comp(*i, *j) is true. As usual, both versions return last1 if no such iterator i exists.distance1*distance2 comparisons or applications of comp (where distanceX is the distance between firstX and lastX).#include <iostream> #include <vector> #include <algorithm> using namespace std; int main() { int a1[] = {1, 2, 333, 3, 4, 5, 6, 444, 7, 8, 9, 10}; vector<int> v1(a1, a1+12); 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[] = {333, 444, 555}; 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 = find_first_of(v1.begin(), v1.end(), v2.begin(), v2.end()); if (p != v1.end()) cout <<"\nThe first instance of a value from v2 occurring in v1 happens at location " <<(int)(p-v1.begin()+1)<<"."; else cout <<"\nNo instance of v2 was found in v1."; p = find_first_of(p+1, v1.end(), v2.begin(), v2.end()); if (p != v1.end()) cout <<"\nThe next instance of a value from v2 occurring in v1 happens at location " <<(int)(p-v1.begin()+1) << "."; else cout <<"\nNo further instance of a value from v2 occurring in v1 was found."; p = find_first_of(p+1, v1.end(), v2.begin(), v2.end()); if (p != v1.end()) cout <<"\nThe next instance of a value from v2 occurring in v1 happens at location " <<(int)(p-v1.begin()+1)<<"."; else cout <<"\nNo further instance of a value from v2 occurring in v1 was found."; return 0; }
You must login to leave a messge