How to Insert a Range of Elements in a Set in C++ STL?
Prerequisites: Set in C++
Sets in C++ are a type of associative container in which each element has to be unique because the value of the element identifies it. The values are stored in a specific sorted order i.e. either ascending or descending.
Some Basic Functions Associated with Set:
- begin(): Returns an iterator to the first element in the set.
- end(): Returns an iterator to the theoretical element that follows the last element in the set.
- size(): Returns the number of elements in the set.
- max_size(): Returns the maximum number of elements that the set can hold.
- empty(): Returns whether the set is empty.
Inserting an Iterator Range into a Set
It takes the range and inserts the element one by one into the BST if it is not already present in it in O(N log(N)) time.
set_name.insert(iterator Starting_position, iterator Ending_position);
Note: It insert element of Starting_position, Ending_position) which means Ending_position element is excluded.
Inserting an array of Size: 5 Size of set: 5 1 2 3 4 5
Inserting a vector of Size: 5 Size of set: 5 1 2 3 4 5