Count element in a sequence with algorithm count - C++ STL Algorithm

C++ examples for STL Algorithm:count

Description

Count element in a sequence with algorithm count

Demo Code

#include <iostream> 
#include <algorithm> // algorithm definitions 
#include <numeric> // accumulate is defined here 
#include <vector> 
#include <iterator> 
using namespace std; 

int main() //from   w ww  . j a  v a 2  s .  co m
{ 
   const int SIZE = 10; 
   int a1[ SIZE ] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }; 
   vector< int > v( a1, a1 + SIZE ); // copy of a1 
   ostream_iterator< int > output( cout, " " ); 

   int a2[ SIZE ] = { 100, 2, 8, 1, 50, 3, 8, 8, 9, 10 }; 
   vector< int > v2( a2, a2 + SIZE ); // copy of a2 
   cout << "\n\nVector v2 contains: "; 
   copy( v2.begin(), v2.end(), output ); 

   // count number of elements in v2 with value 8 
   int result = count( v2 .begin(), v2.end(), 8 ); 
   cout << "\nNumber of elements matching 8: " << result; 

}

Result


Related Tutorials