How to Add an Element to a Set in C++? Last Updated : 26 Feb, 2024 Summarize Comments Improve Suggest changes Share Like Article Like Report 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 element to a std::set in C++, we can use the std::set::insert() function. This function takes a value as an argument and inserts it into the set and maintains the set’s sorted order. C++ Program to Add an Element to a Set C++ // CPP program to add an element to a Set #include <iostream> #include <set> using namespace std; // Driver Code int main() { set<int> mySet = { 1, 2, 4, 5, 6 }; // Adding element 3 to the set using insert() function mySet.insert(3); // Displaying the elements of the set for (int elem : mySet) { cout << elem << " "; } return 0; } // This code is contributed by Susobhan Akhuli Output1 2 3 4 5 6 Time Complexity: O(log n), where n is the number of elements in the set.Auxiliary Space: O(1) We can also elements to the set by using the emplace() function. Comment More infoAdvertise with us Next Article How to Access an Element in Set in C++? S susobhanakhuli19 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 Multiple Elements to a Set in C++? 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 S 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 How to Access an Element in Set in C++? In C++ STL (Standard Template Library), the set container represents a collection of unique, sorted elements. In this article, we will see how to an element in a set in C++ using iterators. Example Input: mySet = {1, 8, 99, 444, 521 } Output: mySet Value at Index 2: 99Access an Element in a Set in C 2 min read How to Add an Element at the Front of a List in C++? In C++, the STL has a doubly linked list container defined as the std::list class template inside the <list> header. It stores the sequential data in non-contiguous memory locations. In this article, we will learn how to add an element at the beginning of a list in C++ STL. Example: Input: myL 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 Like