Answers for "stl count c++"

C++
1

count function c++

1 #include <iostream>
2 #include <array>
3 #include <algorithm>
4
5 using namespace std;
6
7 int main(){
8 array<int, 5> nums = {1, 2, 3, 100, 2};
9 //counting number of twos
10 int numOccurrences = count(nums.begin(), nums.end(), 2);
11 cout << 2 << " appeared " << numOccurrences << " times" << endl;
12 return 0;
13 }
14
15 /*
16 2 appeared 2 times
17 */
Posted by: Guest on May-25-2020
0

C++ std::count function

// count algorithm example
#include <iostream>     // std::cout
#include <algorithm>    // std::count
#include <vector>       // std::vector

int main () {
  // counting elements in array:
  int myints[] = {10,20,30,30,20,10,10,20};   // 8 elements
  int mycount = std::count (myints, myints+8, 10);
  std::cout << "10 appears " << mycount << " times.n";

  // counting elements in container:
  std::vector<int> myvector (myints, myints+8);
  mycount = std::count (myvector.begin(), myvector.end(), 20);
  std::cout << "20 appears " << mycount  << " times.n";

  return 0;
}
Posted by: Guest on August-10-2021

Browse Popular Code Answers by Language