
- C++ Library - Home
- C++ Library - <fstream>
- C++ Library - <iomanip>
- C++ Library - <ios>
- C++ Library - <iosfwd>
- C++ Library - <iostream>
- C++ Library - <istream>
- C++ Library - <ostream>
- C++ Library - <sstream>
- C++ Library - <streambuf>
- C++ Library - <atomic>
- C++ Library - <complex>
- C++ Library - <exception>
- C++ Library - <functional>
- C++ Library - <limits>
- C++ Library - <locale>
- C++ Library - <memory>
- C++ Library - <new>
- C++ Library - <numeric>
- C++ Library - <regex>
- C++ Library - <stdexcept>
- C++ Library - <string>
- C++ Library - <thread>
- C++ Library - <tuple>
- C++ Library - <typeinfo>
- C++ Library - <utility>
- C++ Library - <valarray>
- The C++ STL Library
- C++ Library - <array>
- C++ Library - <bitset>
- C++ Library - <deque>
- C++ Library - <forward_list>
- C++ Library - <list>
- C++ Library - <map>
- C++ Library - <multimap>
- C++ Library - <queue>
- C++ Library - <priority_queue>
- C++ Library - <set>
- C++ Library - <stack>
- C++ Library - <unordered_map>
- C++ Library - <unordered_set>
- C++ Library - <vector>
- C++ Library - <algorithm>
- C++ Library - <iterator>
- The C++ Advanced Library
- C++ Library - <any>
- C++ Library - <barrier>
- C++ Library - <bit>
- C++ Library - <chrono>
- C++ Library - <cinttypes>
- C++ Library - <clocale>
- C++ Library - <condition_variable>
- C++ Library - <coroutine>
- C++ Library - <cstdlib>
- C++ Library - <cstring>
- C++ Library - <cuchar>
- C++ Library - <charconv>
- C++ Library - <cfenv>
- C++ Library - <cmath>
- C++ Library - <ccomplex>
- C++ Library - <expected>
- C++ Library - <format>
- C++ Library - <future>
- C++ Library - <flat_set>
- C++ Library - <flat_map>
- C++ Library - <filesystem>
- C++ Library - <generator>
- C++ Library - <initializer_list>
- C++ Library - <latch>
- C++ Library - <memory_resource>
- C++ Library - <mutex>
- C++ Library - <mdspan>
- C++ Library - <optional>
- C++ Library - <print>
- C++ Library - <ratio>
- C++ Library - <scoped_allocator>
- C++ Library - <semaphore>
- C++ Library - <source_location>
- C++ Library - <span>
- C++ Library - <spanstream>
- C++ Library - <stacktrace>
- C++ Library - <stop_token>
- C++ Library - <syncstream>
- C++ Library - <system_error>
- C++ Library - <string_view>
- C++ Library - <stdatomic>
- C++ Library - <variant>
- C++ STL Library Cheat Sheet
- C++ STL - Cheat Sheet
- C++ Programming Resources
- C++ Programming Tutorial
- C++ Useful Resources
- C++ Discussion
C++ Unordered_set::count() Function
The C++std::unordered_set::count() function is used to count the number of particular elements or the specified key k in the unordered_set container. It does not accept any duplicate value , so if the key is present in the set it returns 1, otherwise 0.
The unordered_set is an associative container that contains a set of unique objects of type key. Every operation like insertion, search, and removal in an unordered_set has constant-time complexity.
Syntax
Following is the syntax of std::unordered_set::count() function.
size_type count ( const key_type& k ) const;
Parameters
- k − It indicates the search element.
Return Value
It returns 1 if an element with a value equivalent to k is found in unordered_set container, otherwise zero.
Example 1
Let's look at the following example, where we are going to demonstrate the usage of count() function.
#include <iostream> #include <string> #include <unordered_set> int main () { std::unordered_set<std::string> myset = { "sairam", "krishna", "prasad" }; for (auto& x: {"tutorialspoint","sairam","krishna","t-shirt"}) { if (myset.count(x)>0) std::cout << "myset has " << x << std::endl; else std::cout << "myset has no " << x << std::endl; } return 0; }
Output
Let us compile and run the above program, this will produce the following result −
myset has no tutorialspoint myset has sairam myset has krishna myset has no t-shirt
Example 2
Consider the following example, where we are going to use the unordered_set with integer type and going to check whether the element exists or not.
#include <iostream> #include <string> #include <unordered_set> using namespace std; int main () { unordered_set<int> myUset = { 10, 20, 30, 50, 60 }; cout<<"following is the element of the unordered_set!"<<endl; for(auto it:myUset){ cout<<it<<endl; } cout<<"is 10 present in the set: "<<myUset.count(20)<<endl; cout<<"is 70 present in the set: "<<myUset.count(70)<<endl; return 0; }
Output
If we run the above code it will generate the following output −
following is the element of the unordered_set! 60 50 30 20 10 is 10 present in the set: 1 is 70 present in the set: 0
Example 3
In the following example, we are going to use the unordered_set with type string and applying the count() function to check whether the element exists or not and displaying the if statement based on the conditions.
#include <iostream> #include <string> #include <unordered_set> using namespace std; int main () { unordered_set<string> myUset = { "tutorialspoint", "India", "PVT", "LTD" }; cout<<"following is the element of the unordered_set!"<<endl; for(auto it:myUset){ cout<<it<<" "; } cout<<endl; if(myUset.count("tutorialspoint") == 1) cout<<"tutorialspoint is present in the set: "<<endl; else cout<<"tutorialspoint is not present in the set: "<<endl; return 0; }
Output
Following is the output of the above code −
following is the element of the unordered_set! LTD PVT India tutorialspoint tutorialspoint is present in the set:
Example 4
Following the another example of using the count() function with unordered_set of string type and checking the whether the element exists or not.
#include <iostream> #include <string> #include <unordered_set> using namespace std; bool isPresent(string s, char ch) { unordered_set<char> set; for(int i=0; i<s.size(); i++){ set.insert(s[i]); } if(set.count(ch) == 1){ cout<<"char "<<ch<<" is present so it will print: "; return true; } cout<<"char "<<ch<<" is not present so it will print: "; return false; } int main() { cout << isPresent("tutorialspoint", 't') << '\n'; return 0; }
Output
Output of the above code is as follows −
char t is present so it will print: 1