How to Insert Elements into a Set Using Iterator in C++? Last Updated : 09 Feb, 2024 Summarize Comments Improve Suggest changes Share Like Article Like Report In C++, a set is a container provided by the Standard Template Library(STL) that stores unique elements of the same type in a sorted order. In this article, we will learn how to use an iterator to insert elements into a set in C++. Example: Input: myVector = {10, 20, 30, 40, 50} Output: myVector = {10, 20, 30, 40, 50, 60, 70, 80}Insert Elements into a Set Using Iterator in C++ In C++, the std::set contains the std::set::insert() member function that also accepts iterators denoting a range of elements to be inserted in the set. These iterators can belong to any data container such as vector, deque, and even another set. C++ Program to Insert Elements into a Set Using Iterator C++ // C++ program to insert elements into a set using iterator #include <iostream> #include <set> #include <vector> using namespace std; int main() { // initialzie a set set<int> s = { 10, 20, 30, 40, 50 }; // initialize a vector whose elements will be added into // the set vector<int> vec = { 60, 70, 80 }; // Printing the elements of the set before insertion cout << "Before Insertion:"; for (const auto& element : s) { cout << element << " "; } cout << endl; // Inserting elements into the set using iterators s.insert(vec.begin(), vec.end()); // Printing the elements of the set after insertion cout << "After Insertion:"; for (const auto& element : s) { cout << element << " "; } cout << endl; return 0; } OutputBefore Insertion:10 20 30 40 50 After Insertion:10 20 30 40 50 60 70 80 Time Complexity: O(M logN) where N is the number of elements in the set and M is the number of elements in the vector.Auxilary Space: O(M) Note: If you are looking for a method to insert element at a particular position in a set using iterator, it is not possible as set always store data in some order. Comment More infoAdvertise with us Next Article How to Access Vector Element Using Iterator in C++? G gaurav472 Follow Improve Article Tags : C++ Programs C++ STL cpp-set CPP Examples +1 More Practice Tags : CPPSTL Similar Reads How to Insert into Vector Using Iterator in C++? In C++, a vector is a dynamic array that can grow and shrink in size as needed. Vectors are sequence containers representing arrays that can change in size. In this article, we will learn how to insert elements into a vector using an iterator in C++. Example: Input: myVector = {10,20,30}; Output: Ve 2 min read How to Insert an Element into a Multiset in C++? In C++, multisets are associative containers similar to sets, but unlike sets, they allow the users to store duplicate elements. In this article, we will learn how we can insert an element into a multiset in C++. Example: Input: myMultiset ={1,2,4,5,6,7,8} Output: myMultiset = {1,2,3,4,5,6,7,8} // i 2 min read How to Insert Multiple Elements to a Multiset in C++? In C++, a multiset is a container that stores elements in a specific order. Multiple elements can have the same values. In this article, we will learn how to insert multiple elements into a multiset in C++. Example: Input: myMultiset = {1, 4, 5, 9}; ElementsToBeAdded = {2, 3, 4} Output: myMultiset = 2 min read How to Access Vector Element Using Iterator in C++? In C++, vectors are container that works like a dynamic array. STL provides iterators for the traversal of these containers. In this article, we will learn how to access an element in a vector using an iterator in C++. Example Input: myVector = {1, 5, 8, 1, 3, 4} Output: myVector value at index 5 us 2 min read How to Access First Element in a Vector Using Iterator in C++? In C++, vectors are containers similar to arrays but unlike arrays, vectors can resize themselves during the runtime. These vectors provide iterators to access their elements. In this article, we will learn how to access the first element in a vector using an iterator in C++. Example Input:myVector 2 min read How to Access Elements in Set by Index in C++? In C++, elements of a set cannot be accessed directly by index or position. However, we can work around this limitation using iterators. In this article, we will learn how to access the elements in set by index in C++.The most efficient way to access a set element by index is to use the std::next() 3 min read Like