unordered_set insert() function in C++ STL Last Updated : 23 Jun, 2022 Comments Improve Suggest changes Like Article Like Report The unordered_set::insert() is a built-in function in C++ STL which is used to insert a new {element} in the unordered_set container. Each element is inserted only if it is not already present in the container (elements in an unordered_set have unique values). The insertion is done automatically at the position according to the container’s criterion (since it uses different hashing functions). This effectively increases the container size by the number of elements inserted.Syntax: unordered_set_name.insert (Value) or, unordered_set_name.insert (InputIterator first, InputIterator last) Parameters: Value: It specifies the value which is to be inserted in the container.first, last: Iterators specifying a range of elements. Copies of the elements in the range [first, last) are inserted in the unordered_set container. Keep in mind that the range includes all the elements between first and last, including the element pointed by first but not the one pointed by last. Return Value: The function returns a pair, with its member pair::first set to an iterator pointing to either the newly inserted element or to the equivalent element already in the set. The pair::second element in the pair is set to true if a new element was inserted or false if an equivalent element already existed. Below are programs that illustrate the above function:Time Complexity: insert() method takes O(1). Program 1: CPP #include<iostream> #include <string> #include <unordered_set> using namespace std; int main() { unordered_set<string> mySet = { "first", "third" }; string myString = "tenth"; // inserts key in set mySet.insert(myString); cout << "My set contains:" << endl; for (const string& x : mySet) { cout << x << " "; } cout << endl; return 0; } OutputMy set contains: tenth first third Program 2: CPP // C++ program to illustrate // unordered_set::insert() #include <array> #include <iostream> #include <string> #include <unordered_set> using namespace std; int main() { unordered_set<std::string> mySet = { "first", "third", "second" }; array<std::string, 2> myArray = { "tenth", "seventh" }; string myString = "ninth"; mySet.insert(myString); // array elements range insertion in set mySet.insert(myArray.begin(), myArray.end()); // initializer list insertion mySet.insert({ "fourth", "sixth" }); cout << "myset contains:" << endl; for (const string& x : mySet) { cout << x << " "; } cout << endl; return 0; } Outputmyset contains: sixth fourth seventh first tenth second third ninth Program 3: C++ // C++ program to illustrate // unordered_set::insert() return values #include <iostream> #include <bits/stdc++.h> using namespace std; //function to display the elements of the unordered set void display_elements(unordered_set<int> &u_set) { cout<<"the elements int the unordered set are: "; for(auto it:u_set) { cout<<it <<" "; } cout<<endl; } int main() { unordered_set<int> u_set; cout<<"u_set.insert(1).second: "<<u_set.insert(1).second<<endl; //on successful insertion it's true else false. cout<<"*(u_set.insert(1).first): "<<*(u_set.insert(1).first)<<endl; //first is the iterator to the inseted element, if the element not present in the u_set, //if the element already in the u_set, then it points to that element cout<<"u_set.insert(1).second: "<<u_set.insert(1).second<<endl; cout<<"*(u_set.insert(1).first): "<<*(u_set.insert(1).first)<<endl; cout<<"u_set.insert(2).second: "<<u_set.insert(2).second<<endl; //on successful insertion it's true else false. cout<<"*(u_set.insert(2).first): "<<*(u_set.insert(2).first)<<endl; display_elements(u_set); return 0; } Outputu_set.insert(1).second: 1 *(u_set.insert(1).first): 1 u_set.insert(1).second: 0 *(u_set.insert(1).first): 1 u_set.insert(2).second: 1 *(u_set.insert(2).first): 2 the elements int the unordered set are: 2 1 Comment More infoAdvertise with us A AkshitaSaraf Follow Improve Article Tags : Technical Scripter C++ Technical Scripter 2018 Practice Tags : CPP Explore C++ Programming Language 5 min read C++ OverviewIntroduction to C++ Programming Language 3 min read Features of C++ 5 min read History of C++ 7 min read Interesting Facts about C++ 2 min read Setting up C++ Development Environment 8 min read Difference between C and C++ 3 min read C++ BasicsUnderstanding First C++ Program 4 min read C++ Basic Syntax 4 min read C++ Comments 3 min read Tokens in C 4 min read C++ Keywords 2 min read Difference between Keyword and Identifier in C 3 min read C++ Variables and ConstantsC++ Variables 4 min read Constants in C 4 min read Scope of Variables in C++ 7 min read Storage Classes in C++ with Examples 6 min read Static Keyword in C++ 5 min read C++ Data Types and LiteralsC++ Data Types 7 min read Literals in C 4 min read Derived Data Types in C++ 4 min read User Defined Data Types in C++ 4 min read Data Type Ranges and Their Macros in C++ 3 min read C++ Type Modifiers 4 min read Type Conversion in C++ 4 min read Casting Operators in C++ 5 min read C++ OperatorsOperators in C++ 9 min read C++ Arithmetic Operators 4 min read Unary Operators in C 5 min read Bitwise Operators in C 6 min read Assignment Operators in C 4 min read C++ sizeof Operator 3 min read Scope Resolution Operator in C++ 4 min read C++ Input/OutputBasic Input / Output in C++ 5 min read cin in C++ 4 min read cout in C++ 2 min read Standard Error Stream Object - cerr in C++ 2 min read Manipulators in C++ 4 min read C++ Control StatementsDecision Making in C (if , if..else, Nested if, if-else-if ) 7 min read C++ if Statement 3 min read C++ if else Statement 3 min read C++ if else if Ladder 3 min read Switch Statement in C++ 5 min read Jump statements in C++ 4 min read C++ Loops 7 min read for Loop in C++ 6 min read Range-Based for Loop in C++ 3 min read C++ While Loop 3 min read C++ do while Loop 4 min read C++ FunctionsFunctions in C++ 8 min read return Statement in C++ 4 min read Parameter Passing Techniques in C 3 min read Difference Between Call by Value and Call by Reference in C 4 min read Default Arguments in C++ 5 min read Inline Functions in C++ 6 min read Lambda Expression in C++ 4 min read C++ Pointers and ReferencesPointers and References in C++ 5 min read C++ Pointers 8 min read Dangling, Void , Null and Wild Pointers in C 6 min read Applications of Pointers in C 4 min read Understanding nullptr in C++ 3 min read References in C++ 5 min read Can References Refer to Invalid Location in C++? 2 min read Pointers vs References in C++ 5 min read Passing By Pointer vs Passing By Reference in C++ 5 min read When do we pass arguments by pointer? 5 min read Like