Go back to Richel Bilderbeek's homepage.

Go back to Richel Bilderbeek's C++ page.

 

 

 

 

 

(C++) std::count

 

std::count is an STL algorithm for counting elements in a container. For conditional counting use std::count_if.

 

#include <algorithm>
#include <cassert>
#include <vector>

int main()
{
  std::vector<int> v;
  v.push_back(1);
  v.push_back(2);
  v.push_back(2);
  v.push_back(3);
  v.push_back(3);
  v.push_back(3);

  assert(std::count(v.begin(),v.end(),1)==1);
  assert(std::count(v.begin(),v.end(),2)==2);
  assert(std::count(v.begin(),v.end(),3)==3);
}

 

 

 

 

 

Go back to Richel Bilderbeek's C++ page.

Go back to Richel Bilderbeek's homepage.

 

Valid XHTML 1.0 Strict