![]() |
VOOZH | about |
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:
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:
My set contains: tenth first third
Program 2:
myset contains: sixth fourth seventh first tenth second third ninth
Program 3:
u_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