Source of find_first_of1a.cpp


  1: //find_first_of1a.cpp

  3: #include <iostream>
  4: #include <vector>
  5: #include <algorithm>
  6: using namespace std;

  8: int main()
  9: {
 10:     cout << "\nThis program illustrates the use of the STL find_first_of() "
 11:         "algorithm (default\nversion) to find the first occurrence of any "
 12:         "one of a range of integer values\nin a vector within another range "
 13:         "of integer values, also in a vector.";
 14:     cout << "\nPress Enter to continue ... ";  cin.ignore(80, '\n');

 16:     int a1[] = {1, 2, 333, 3, 4, 5, 6, 444, 7, 8, 9, 10};
 17:     vector<int> v1(a1, a1+12);
 18:     cout << "\nHere are the contents of v1:\n";
 19:     for (vector<int>::size_type i=0; i<v1.size(); i++)
 20:         cout << v1.at(i) << " ";
 21:     cout << "\nPress Enter to continue ... ";  cin.ignore(80, '\n');

 23:     int a2[] = {333, 444, 555};
 24:     vector<int> v2(a2, a2+3);
 25:     cout << "\nHere are the contents of v2:\n";
 26:     for (vector<int>::size_type i=0; i<v2.size(); i++)
 27:         cout << v2.at(i) << " ";
 28:     cout << "\nPress Enter to continue ... ";  cin.ignore(80, '\n');

 30:     vector<int>::iterator p;

 32:     p = find_first_of(v1.begin(), v1.end(), v2.begin(), v2.end());
 33:     if (p != v1.end())
 34:         cout << "\nThe first instance of a value from v2 occurring in "
 35:             "v1 happens at location "
 36:             << (int)(p-v1.begin()+1) << ".";
 37:     else
 38:         cout << "\nNo instance of v2 was found in v1.";
 39:     cout << "\nPress Enter to continue ... ";  cin.ignore(80, '\n');

 41:     p = find_first_of(p+1, v1.end(), v2.begin(), v2.end());
 42:     if (p != v1.end())
 43:         cout << "\nThe next instance of a value from v2 occurring in "
 44:             "v1 happens at location "
 45:             << (int)(p-v1.begin()+1) << ".";
 46:     else
 47:         cout << "\nNo further instance of a value from v2 occurring in "
 48:         "v1 was found.";
 49:     cout << "\nPress Enter to continue ... ";  cin.ignore(80, '\n');

 51:     p = find_first_of(p+1, v1.end(), v2.begin(), v2.end());
 52:     if (p != v1.end())
 53:         cout << "\nThe next instance of a value from v2 occurring in "
 54:             "v1 happens at location "
 55:             << (int)(p-v1.begin()+1) << ".";
 56:     else
 57:         cout << "\nNo further instance of a value from v2 occurring in "
 58:         "v1 was found.";
 59:     cout << "\nPress Enter to continue ... ";  cin.ignore(80, '\n');
 60: }