Source of count1a.cpp


  1: //count1a.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 count() "
 11:         "algorithm to count\nthe number of times an integer value "
 12:         "appears in a vector of integers.";
 13:     cout << "\nPress Enter to continue ... ";  cin.ignore(80, '\n');

 15:     int a[] ={1, 2, 2, 2, 3, 2, 4, 2, 5, 2};
 16:     vector<int> v(a, a+10);
 17:     cout << "\nHere are the contents of v:\n";
 18:     for (vector<int>::size_type i=0; i<v.size(); i++)
 19:         cout << v.at(i) << " ";
 20:     cout << "\nPress Enter to continue ... ";  cin.ignore(80, '\n');

 22:     cout << "\nThe number of times the value " << 2 << " appears "
 23:         << "in v is " << (int)count(v.begin(), v.end(), 2) << ".";
 24:     cout << "\nPress Enter to continue ... ";  cin.ignore(80, '\n');

 26:     cout << "\nThe number of times the value " << 7 << " appears "
 27:         << "in v is " << (int)count(v.begin(), v.end(), 7) << ".";
 28:     cout << "\nPress Enter to continue ... ";  cin.ignore(80, '\n');
 29: }