Skip to main content

C++ STL Unordered Set – std::unordered_set

In this tutorial you will learn about stl unordered set container in c++ and various functions applicable on it.

Unordered Set comes under unordered containers. As we discussed in introduction to stl article, unordered containers internally implemented with hash tables. Each item calculated by hash function, to map to hash table. The main advantage is if we have effective hash function we can find elements in O (1) time. On average it can go to linear time. Simply we can say it’s based on type of hash function used.

So we can say these are fastest among all containers.

As name says that in unordered_set the order is not defined. Unordered set doesn’t allow duplicates.

C++ STL Unordered Set – std::unordered_set

Useful iterators to work on this unordered set:

begin(): returns iterator to the beginning

end(): returns iterator to the end of the list

cbegin(): returns constant iterator to the beginning

cend(): returns constant iterator to the end.

First we need to include unordered_set header file. Which is #include<unordered_set>

Inserting element into unordered_set:

There are various ways to insert elements into unordered_set.

Method 1: Insert directly by passing element.

UnOrdSet.insert(element);

Method 2: Using iterator. This returns iterator at inserted position.

UnOrdSet.insert ( iterator, value)

Method 3: Copying from another container.

Example program to insert into unordered_set:

#include<iostream>
#include <unordered_set>

using namespace std;

int main(){
        unordered_set<int> s1; // declaring unordered_set
        unordered_set<int> :: iterator it; // iterator for unordered_set
        
        for(int i=0;i<5;i++){
                s1.insert(i*10); // inserting using Method1
        }
        
        it= s1.begin();
        s1.insert(it,99);
        
        int ary[]= { 23, 34, 45, 56};
        s1.insert(ary, ary+4); // Inserting using method3
        
        //checking by printing
        for(it= s1.begin(); it!=s1.end(); it++) cout << *it << " ";
        
        // We can observe that output will be print in sorted order. That is the property of Unordered_set
        
        return 0;
}

Output

56 45 34 23 40 99 0 10 20 30

Modifiers: The functions which effect size/data of that container

erase(): We can erase an element by specifying value or pointing to iterator.

swap(): swaps elements of Unordered_set1 to Unorderedset2 and Unorderedset2 to Unordered_set1.

clear(): removes all elements in the list. It results list of size 0.

Example program to show above functions:

#include<iostream>
#include<unordered_set>

using namespace std;

int main(){
        unordered_set<int> s1;
        unordered_set<int> :: iterator it;
        
        for(int i=0; i<5; i++) s1.insert(i+10);
        s1.erase(12); // deleting element 12
        cout << "elements after deleting  12 -->";
        for(it= s1.begin(); it!=s1.end(); it++) cout << *it << " ";
        cout << endl;
        
        unordered_set<int> s2;
        for(int i=0;i<4;i++) s2.insert(i);
        
        cout << "unordered_set 1 elements before swapping -->";
        for(it= s1.begin(); it!= s1.end(); it++) cout<< *it << " ";
        cout << endl;
        
        cout << "unordered_set 2 elements before swapping -->";
        for(it= s2.begin(); it!= s2.end(); it++) cout<< *it << " ";
        cout << endl;
        
        s1.swap(s2); // swapping operation

        cout << "unordered_set 1 elements after swapping -->";
        for(it= s1.begin(); it!= s1.end(); it++) cout<< *it << " ";
        cout << endl;
        
        cout << "unordered_set 2 elements after swapping -->";
        for(it= s2.begin(); it!= s2.end(); it++) cout<< *it << " ";
        cout << endl;

        s1.clear(); // clearing list 1
        cout << "Performing clear() operation on unordered_set1......" << endl;
        s1.empty() ? cout <<"Unordered_set is empty" << endl: cout << "unordered_set is not empty" << endl; //
        // ternary operation which resutls list is empty or not
        
        return 0;
}

Output

elements after deleting 12 –>14 10 11 13
unordered_set 1 elements before swapping –>14 10 11 13
unordered_set 2 elements before swapping –>3 2 1 0
unordered_set 1 elements after swapping –>3 2 1 0
unordered_set 2 elements after swapping –>14 10 11 13
Performing clear() operation on unordered_set1……
Unordered_set is empty

Information retrieving functions:

empty(): returns a Boolean value whether Unordered_set is empty or not.

size(): returns the size of the Unordered_set.

max_size(): returns the maximum size a Unordered_set can have.

And some more operations are:

find(): It returns iterator to the element.

count(x):  Returns how many times elements “x” present in Unordered_set.

Example program to show above functions:

#include<iostream>
#include<unordered_set>

using namespace std;

int main(){
        unordered_set<int> s1;
        unordered_set<int> :: iterator it;
        
        for(int i=0; i<5; i++) s1.insert(i+10);
        s1.empty() ? cout <<"Unordered_set is empty" << endl: cout << "unordered_set is not empty" << endl;
        cout << "size of the unordered_set is " << s1.size() << endl;
        cout << "maximum size of the unordered_set is " << s1.max_size() << endl;
        cout << "finding elemnt 12 in unordered_set" << endl;
        it= s1.find(12);
        cout << *it << endl;
        
        s1.insert(12);
        if(s1.count(22)) cout << "number 22 is in the unordered_set " << endl;
        else cout << "22 is not in the unordered_set";
        
        return 0;
}

Output

unordered_set is not empty
size of the unordered_set is 5
maximum size of the unordered_set is 1152921504606846975
finding elemnt 12 in unordered_set
12
22 is not in the unordered_set

The post C++ STL Unordered Set – std::unordered_set appeared first on The Crazy Programmer.



from The Crazy Programmer https://www.thecrazyprogrammer.com/2017/10/stl-unordered-set.html

Comments

Popular posts from this blog

dotnet sdk list and dotnet sdk latest

Can someone make .NET Core better with a simple global command? Fanie Reynders did and he did it in a simple and elegant way. I'm envious, in fact, because I spec'ed this exact thing out in a meeting a few months ago but I could have just done it like he did and I would have used fewer keystrokes! Last year when .NET Core was just getting started, there was a "DNVM" helper command that you could use to simplify dealing with multiple versions of the .NET SDK on one machine. Later, rather than 'switching global SDK versions,' switching was simplified to be handled on a folder by folder basis. That meant that if you had a project in a folder with no global.json that pinned the SDK version, your project would use the latest installed version. If you liked, you could create a global.json file and pin your project's folder to a specific version. Great, but I would constantly have to google to remember the format for the global.json file, and I'd constan

R vs Python for Machine Learning

There are so many things to learn before to choose which language is good for Machine Learning. We will discuss each and everything about R as well as Python and the situation or problem in which situation we have to use which language. Let’s start Python and R are the two most Commonly used Programming Languages for Machine Learning and because of the popularity of both the languages Novice or you can say fresher are getting confused, whether they should choose R or Python language to commence their career in the Machine learning domain. Don’t worry guys through this article we will discuss R vs Python for Machine Learning. So, without exaggerating this article let’s get started. We will start it from the very Basics things or definitions. R vs Python for Machine Learning Introduction R is a programming language made by statisticians and data miners for statistical analysis and graphics supported by R foundation for statistical computing. R also provides high-quality graphics and

Top Tips For PCB Design Layout

Are you thinking about designing a printed circuit board? PCBs are quite complicated, and you need to make sure that the layout that you choose is going to operate as well as you want it to. For this reason, we have put together some top tips for PCB design layout. Keep reading if you would like to find out more about this. Leave Enough Space One of the most important design tips for PCB layout is that you need to make sure that you are leaving enough space between the components. While many people might think that packing components closely is the best route to take, this can cause problems further down the line. This is why we suggest leaving extra space for the wires that will spread. This way, you’ll have the perfect PCB design layout. Print Out Your Layout Struggling to find out if your components sizes match? Our next tip is to print out your layout and compare the printed version to your actual components. Datasheets can sometimes come with errors, so it doesn’t hurt to do