This C++ program demonstrates the find_first_of() algorithm. The function find_first_of() finds the first element from first container which exists in second container. The function takes four iterators – iterators to the beginning and end to the sequence containers.
Here is the source code of the C++ program which demonstrates the find_first_of() algorithm. The C++ program is successfully compiled and run on a Linux system. The program output is also shown below.
/*
* C++ program to demonstrate find_first_of algorithm
*/
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <functional>
#include <iomanip>
using namespace std;
void print(char a[], int N)
{
for(int i = 0; i < N; i++)
{
cout << setw(2) << a[i] << " ";
}
cout << endl;
}
int main()
{
char a[] = {'a', 'e', 'i', 'o', 'u'};
char b[] = {'z', 'y', 'd', 'e', 'f'};
int alen = sizeof(a) / sizeof(char), blen = sizeof(b) / sizeof(char);
char * i;
cout << "Array a : ";
print(a, alen);
cout << "Array b : ";
print(b, blen);
// Without any predicate
i = find_first_of(a, a + alen, b, b + blen);
cout << "Iterator to first matching sequence : " << *i
<< endl;
// With predicate less
i = find_first_of(b, b + blen, a, a + alen, less<char>());
cout << "Iterator to first character in b less than character in a : "
<< *i << endl;
}
$ a.out Array a : a e i o u Array b : z y d e f Iterator to first matching sequence : e Iterator to first character in b less than character in a : d
Sanfoundry Global Education & Learning Series – 1000 C++ Programs.
advertisement
advertisement
If you wish to look at all C++ Programming examples, go to C++ Programs.
Related Posts:
- Check C++ Books
- Check Programming Books
- Practice Computer Science MCQs
- Apply for C++ Internship
- Practice Programming MCQs