How to Insert elements in an unordered_set in C++11

std::unordered_set provides the different overloaded versions of insert() member function to insert elements i.e.

pair<iterator,bool> insert ( const value_type& val );
void insert ( InputIterator first, InputIterator last );
void insert ( initializer_list<value_type> il );

Let’s see each of them in detail

Inserting element in unordered_set by value

For this we will use basic version of insert() member function i.e.

pair<iterator,bool> insert ( const value_type& val );

Insert() function will try to insert passed argument into the unordered_set and it returns a pair of an iterator and bool flag i.e.

std::pair<std::unordered_set<T>::iterator, bool >

What does this pair contain?

If an element equivalent to passed element is not present in the set, then new element will be inserted successfully and it will return the pair with following values,

  • Iterator will point to newly inserted element
  • Bool flag will be true.

If an element equivalent to passed element is already present in set, then new element will be not be inserted in set and it will return the pair with following values,

  • Iterator will point to the position of first matched equivalent element
  • Bool flag will be false.

Example,

Declare an unordered_set and insert few values i.e.

// Creating an unoredered_set of int
std::unordered_set<int> setOfNum;

// Inserting few elements in set
setOfNum.insert(2);
setOfNum.insert(6);
setOfNum.insert(1);

No try to insert a duplicate element and use the returned value to check id insertion is successful or not i.e.

// Declare a pair of iterator and bool flag
std::pair<std::unordered_set<int>::iterator, bool > result;

// Try to insert a Duplicate Element, it will return a
// a pair of iterator and bool
result = setOfNum.insert(6);

// Check if element inserted or not
if(result.second == false)
	std::cout<<"Failed to Insert 6"<<std::endl;

As element with value 6 was already present therefore it was not inserted again.

Insert elements in unordered_set from an another container or array

std::unordered_set provides an another overloaded version of insert() that accepts a range i.e.

void insert ( InputIterator first, InputIterator last );

Using this we can insert elements from any container or array into the unordered_set. Let’s see how to insert elements from a vector into the unordered_set i.e.

//Create an another vector of ints
std::vector<int> vec({3,4,1,2,9,1,3,4,5,6});

// Creating an unoredered_set of int
std::unordered_set<int> setOfNum;

// Insert the contents of a vector in existing unoordered set
setOfNum.insert(vec.begin(), vec.end());

This will insert all the elements from vector to the unordered_set. But, as unordered_set can contain the unique elements only.

[showads ad=inside_post]

 

Therefore, only following elements will be stored in set i.e.

3,4,1,2,9,5,6

Insert elements in unordered_set from an initializer_list

std::unordered_set<int> setOfNum;

// Insert an initializer_list in existing  unordered set
setOfNum.insert({2,3,6,2,15,17,11});

Complete working example is as follows,

#include <iostream>
#include <unordered_set>
#include <algorithm>

int main() {

	// Creating an unoredered_set of int
	std::unordered_set<int> setOfNum;

	// Inserting few elements in set
	setOfNum.insert(2);
	setOfNum.insert(6);
	setOfNum.insert(1);

	// Declare a pair of iterator and bool flag
	std::pair<std::unordered_set<int>::iterator, bool> result;

	// Try to insert a Duplicate Element, it will return a
	// a pair of iterator and bool
	result = setOfNum.insert(6);

	// Check if element inserted or not
	if (result.second == false)
		std::cout << "Failed to Insert 6" << std::endl;

	// Try to insert a another Element, it will return a
	// a pair of iterator and bool
	result = setOfNum.insert(7);

	if (result.second)
		std::cout << "Successfully Inserted 7" << std::endl;

	// Iterate over the set and display contents
	for (int val : setOfNum)
		std::cout << val << ", ";
	std::cout << std::endl;

	//Create an another vector of ints
	std::vector<int> vec( { 3, 4, 1, 2, 9, 1, 3, 4, 5, 6 });

	// Insert the contents of a vector in existing unoordered set
	setOfNum.insert(vec.begin(), vec.end());

	// Iterate over the set and display contents
	for (int val : setOfNum)
		std::cout << val << ", ";
	std::cout << std::endl;

	// Insert an initializer_list in existing  unordered set
	setOfNum.insert( { 2, 3, 6, 2, 15, 17, 11 });

	// Iterate over the set and display contents
	for (int val : setOfNum)
		std::cout << val << ", ";
	std::cout << std::endl;

}

Output:

Failed to Insert 6
Successfully Inserted 7
7, 1, 6, 2, 
5, 9, 4, 3, 2, 6, 1, 7, 
11, 17, 15, 5, 9, 4, 3, 2, 6, 1, 7,

 

Leave a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to Top