STL count in C++
Level 4240 keystrokes
Uses count to count how many elements equal 2 (4) and count_if with a lambda to count the even elements (5).
C++ code
1vector<int> nums = {1, 2, 2, 3, 2, 4, 2, 5};// count returns how many elements are equal to 22int c = count(nums.begin(), nums.end(), 2);// Print Count of 2: 43cout << "Count of 2: " << c << endl;// count_if counts the elements for which the predicate returns true4int even = count_if(nums.begin(), nums.end(), // Predicate: true for even numbers5 [](int n) { return n % 2 == 0; });// The four 2s plus 4 are even: prints Even: 56cout << "Even: " << even << endl;Output
Count of 2: 4 Even: 5
Practice typing this code. Results are not recorded in the rankings.
Symbols in this code
The symbols you type in this C++ code and how often they appear.
<× 13,× 11(× 7)× 7;× 6=× 5.× 4"× 4:× 2{× 2