count() in C++ STL Last Updated : 27 Nov, 2024 Comments Improve Suggest changes Like Article Like Report In C++, the count() is a built-in function used to find the number of occurrences of an element in the given range. This range can be any STL container or an array. In this article, we will learn about the count() function in C++.Let’s take a quick look at a simple example that uses count() method: C++ #include <bits/stdc++.h> using namespace std; int main() { vector<int> v = {2, 3, 2, 1, 5, 4, 2}; // Count the occurrence of 2 cout << count(v.begin(), v.end(), 2); return 0; } Output3This article covers the syntax, usage, and common examples of count() function in C++:Table of ContentSyntax of count()Examples of count()Count of Given String in Vector of StringsCount the Frequency of Given Element in MultisetCheck Whether the Given Element ExistsSyntax of count()The count() function is defined inside the <algorithm> header file.count(first, last, val);Parametersfirst: Iterator to the first element of the range.last: Iterator to the element just after the last element of the range.val: Value to be counted.Return ValueIf the value found, it returns the number of its occurrences.Otherwise, it returns 0.Examples of count()The following examples demonstrates the use of count() function for different purposes:Count of Given String in Vector of Strings C++ #include <bits/stdc++.h> using namespace std; int main() { vector<string> v = {"Hi", "Geeks", "GeeksforGeeks", "Geeks"}; // Count the occurrence of "Geeks" cout << count(v.begin(), v.end(), "Geeks"); return 0; } Output2Count the Frequency of Given Element in Multiset C++ #include <bits/stdc++.h> using namespace std; int main() { multiset<int> m= {1, 1, 2, 3, 2, 2, 2, 1}; // Counting the frequency of 2 cout << count(m.begin(), m.end(), 2); return 0; } Output4Note: Unique value containers such as set, maps, etc. can only return either 1 or 0 from the count function.Check Whether the Given Element Exists C++ #include <bits/stdc++.h> using namespace std; int main() { vector<int> v = {2, 3, 2, 1, 5, 4, 2}; int val = 7; // Count the occurrence of 7 int c = count(v.begin(), v.end(), val); // Check if the element exists if (c) cout << c; else cout << val << " Not Exists"; return 0; } Output7 Not Exists Comment More infoAdvertise with us Next Article count() in C++ STL J Jatin Goyal Improve Article Tags : C++ STL CPP-Functions cpp-algorithm-library Practice Tags : CPPSTL Similar Reads multimap::count() in C++ STL The multimap::count is a built-in function in C++ STL which returns the number of times a key is present in the multimap container. Syntax: multimap_name.count(key) Parameters: The function accepts one mandatory parameter key which specifies the key whose count in multimap container is to be returne 1 min read strol() function in C++ The strtol() function in C++ interprets the contents of a string as an integral number of the specified base and return its value as a long int.This function also sets an end pointer that points to the first character after the last valid numeric character of the string, if there is no such characte 3 min read Counting Set Bits in C++ Counting set bits means determining how many bits in a binary representation of a number are set to 1.ExampleInput: n = 103 (01100111)Output: 5Explanation: The binary representation of 103 is 01100111. Hence, the number of set bits is 5.Input: n = 15 (1111)Output: 4Explanation: The binary representa 4 min read map count() Function in C++ STL The std::map::count() in C++ is a built-in function that is used to count the occurrence of the given key in the map container. It is the member function of the std::map container.In this article, we will learn how to use std::map::count() in C++.Syntaxmp.count(key)Parameters key: The value whose oc 2 min read set::count() Function in C++ STL The std::set::count() is a built-in function in C++ STL which is used to count the number of times an element occurs in the set container. std::set container stores unique elements, so it can only return 1 or 0. Therefore, it is only used for checking if the element exists in the set or not.ExampleC 3 min read Like