![The C++ Standard Library](/cpp_standard_library/images/cpp-mini-lib-logo.jpg)
- The C Standard Library
- The C Standard Library
- The C++ Standard Library
- 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>
- C++ Programming Resources
- C++ Programming Tutorial
- C++ Useful Resources
- C++ Discussion
C++ Unordered_set::empty() Function
The C++ std::unordered_set::empty() function is used to return a boolean value indicating whether the unordered_set container is empty or not.
If the unordered_set is empty, then begin() is equal to end(), and the empty() function returns true; otherwise, it returns false.
Syntax
Following is the syntax for std::unordered_set::empty.
bool empty() const noexcept;
Parameters
This function does not accepts any parameter.
Return Value
This function returns true if the container size is 0, otherwise false.
Example 1
Consider the following example, where we are going to demonstrate the usage of unordered_set::empty() function.
#include <iostream> #include <string> #include <unordered_set> int main () { std::unordered_set<std::string> first = {"sairam","krishna","mammahe"}; std::unordered_set<std::string> second; std::cout << "first " << (first.empty() ? "is empty" : "is not empty" ) << std::endl; std::cout << "second " << (second.empty() ? "is empty" : "is not empty" ) << std::endl; return 0; }
Output
Let us compile and run the above program, this will produce the following result −
first is not empty second is empty
Example 2
Let's look at the following example, where we are going to use the empty() function to check whether the unordered_set is empty or not.
#include <iostream> #include <string> #include <unordered_set> using namespace std; int main () { unordered_set<string> uSet; if(uSet.empty()==1){ cout<<boolalpha; cout<<"Is unordered_set empty? "<<uSet.empty()<<endl; } else{ cout<<"unordered_set contains following elements: "<<endl; for(auto it: uSet) cout<<it<<endl; } return 0; }
Output
If we run the above code it will generate the following output −
Is unordered_set empty? true
Example 3
In the following example, we are going to use the empty() function to check whether the unordered_set is empty or not and going to display the content of the unordered_set if it contains elements.
#include <iostream> #include <string> #include <unordered_set> using namespace std; int main () { unordered_set<int> uSet{10, 20, 30, 40, 50}; if(uSet.empty()==1){ cout<<boolalpha; cout<<"Is unordered_set empty? "<<uSet.empty()<<endl; } else{ cout<<"unordered_set contains following elements: "<<endl; for(auto it: uSet) cout<<it<<endl; } return 0; }
Output
Following is the output of the above code −
unordered_set contains following elements: 50 40 30 20 10
Example 4
Following is the another example of the usage of the empty() function and displaying the result in the boolean value.
#include <iostream> #include <string> #include <unordered_set> using namespace std; int main () { unordered_set<string> uSet; cout<<boolalpha; cout<<"boolean value before the insertion of elements: "<<endl; cout<<"Is unordered_set empty? "<<uSet.empty()<<endl; uSet.insert("tutorix"); uSet.insert("tutorialspoint"); cout<<"boolean value after the insertion of elements: "<<endl; cout<<"Is unordered_set empty? "<<uSet.empty()<<endl; return 0; }
Output
Output of the above code is as follows −
boolean value before the insertion of elements: Is unordered_set empty? true boolean value after the insertion of elements: Is unordered_set empty? false