How to Add Multiple Elements to a Set in C++? Last Updated : 22 Feb, 2024 Comments Improve Suggest changes Like Article Like Report In C++, a set is a container that stores the unique elements in a sorted order. In this article, we will learn how to add multiple elements to a set in C++. For Example, Input: mySet = {1, 5, 6, 7, 8} Elements to add: {1, 2, 3, 4, 5} Output: Set Elements are: 1 2 3 4 5Insert Multiple Elements in a Set in C++To add multiple elements to a std::set in one go, we can pass an initializer list of elements in the std::set::insert() function using the below syntax: Syntax to Insert Multiple Elements in Set setName.insert({elem1,elem2,elem3,elem4}); C++ Program to Add Multiple Elements to a SetThe below example demonstrates the use of the initializer list and insert() function to add multiple elements in a set. C++ // C++ program to add multiple elements in a set #include <iostream> #include <set> using namespace std; int main() { set<int> mySet; // Adding multiple elements using insert and initializer // list mySet.insert({ 10, 20, 30, 40, 50 }); // Displaying the set cout << "Elements in set are:" << endl; for (const auto& element : mySet) { cout << element << " "; } return 0; } OutputElements in set are: 10 20 30 40 50 Time Complexity: O(n log(n)), here n is the number of elements to be inserted in the set. Auxilliary Space: O(n) Comment More infoAdvertise with us Next Article How to Add Multiple Elements to a Set in C++? A akansha13102003 Follow Improve Article Tags : C++ Programs C++ STL cpp-set CPP Examples +1 More Practice Tags : CPPSTL Similar Reads How to Add Elements to a Map in C++? In C++, a map is an associative container that stores the elements as key-value pairs where each element has a key and a mapped value. In this article, we will learn how to add elements to a map in C++ STL. For Example, Input: myMap = {{1, "Ram"}, {2, "Mohit"}}; Output: myMap = {{1, "Ram"}, {2, "Moh 2 min read How to Add an Element to a Set in C++? In C++ STL, a set is a container that stores unique elements in a sorted order. In this article, we will learn how to add an element to a set in C++ STL. Example: Input: mySet = {1, 2, 4, 5, 8} Element to add: 3 Output: mySet = {1, 2, 3, 4, 5, 8}Add an Element to a Set in C++To add a specific elemen 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 Add Multiple Elements at the End of Vector in C++? In C++, vectors are dynamic arrays that can grow and reduce in size as per requirements. In this article, we will learn how to add multiple elements at the end of a vector in C++. Example Input:myVector = {10, 20, 30, 40, 50}elements_to_add = {60, 70, 80}Output:updated_vector: 10 20 30 40 50 60 70 8 2 min read How to Add Elements in a Vector in C++? In C++, vector provides several built-in methods to insert the elements and efficiency of the insertion depends on the position where the insertion takes place. In this article, we will learn different ways to insert elements into a vector in C++ and also compare their efficiency.The simplest way to 3 min read Like