How to Initialize a Set with an Array in C++? Last Updated : 22 Feb, 2024 Comments Improve Suggest changes Like Article Like Report In C++, an array stores data in contiguous memory locations and can have duplicates as well whereas a set stores unique elements only in a sorted order. In this article, we will learn how to initialize a set with an array in C++. For Example, Input:arr[]={2, 1, 5, 4, 3, 5};Output:Element in Set are: 1 2 3 4 5Initialize a Set With Array in C++To initialize the std::set with the elements of an array, we can use the range constructor of the set and pass the pointers of the beginning and the end of the array. Syntaxstd::set <int> mySet(arr, arr + n);where n is the number of elements in the array. C++ Program to Initialize a Set with an Array The below program demonstrates how we can initialize a set with an element of an array in C++. C++ // C++ program to initialize set with an array elements #include <iostream> #include <set> using namespace std; int main() { // Defining an array int myArray[] = { 2, 1, 5, 4, 3, 5 }; // Initializing the set with the array set<int> mySet(myArray, myArray + 6); // printing the content of set cout << "Elements in a set are:" << endl; for (auto& elem : mySet) { cout << elem << " "; } return 0; } OutputElements in a set are: 1 2 3 4 5 Time Complexity: O(n log(n))Auxilliary Space: O(n) Note: We can also use set::insert() function inside a loop to initialize set with array elements in C++. Comment More infoAdvertise with us Next Article How to Initialize a Set with an Array in C++? adarshsharadpandey23 Follow Improve Article Tags : C++ Programs C++ STL cpp-array cpp-set CPP Examples +2 More Practice Tags : CPPSTL Similar Reads How to Initialize an Array in C++? In C++, an array is a collection of similar datatypes stored in contiguous memory locations in which each element can be accessed using their indices. In this article, we will learn how to initialize an array in C++. Initializing an Array in C++To initialize an array in C++, we can use the assignmen 2 min read How to Initialize a Dynamic Array in C++? In C++, dynamic arrays allow users to allocate memory dynamically. They are useful when the size of the array is not known at compile time. In this article, we will look at how to initialize a dynamic array in C++. Initializing Dynamic Arrays in C++The dynamic arrays can be initialized at the time o 1 min read How to Initialize a Vector with Zero in C++? Initializing a vector with value zero means assigning the initial value 0 to all elements of vector. In this article, we will learn the different methods to initialize the vector with zero in C++.The simplest method to initialize a vector with zeros is by using vector constructor. Let's take a look 2 min read How to Initialize Array With Values Read from a Text File in C++? In C++, arrays store a fixed-size collection of elements of the same type. We can initialize these elements with any valid value. In this article, we will learn to initialize a C++ array with values read from a text file. Example: Input:Text File = âarray.txtâ // contains 1 2 3 4 5Output:// Read the 2 min read How to Initialize Vector of Char Arrays in C++? In C++, a vector is a dynamic array that can grow and shrink in size. A char array is a sequence of characters, typically used to represent strings. In this article, we will learn how to initialize a vector of char arrays in C++. Example: myVector: {âappleâ, âbananaâ, âcherryâ}Initializing a Vector 2 min read How to Initialize 3D Vector in C++ STL? Initializing 3D vector refers to the process of assigning the initial values to the elements of 3D Vector. In this article, we will learn different methods to initialize the 3D vector in C++.The simplest ways to initialize the 3D vector is by passing the elements inside the initializer list. This me 4 min read How to Initialize a Vector with Default Values in C++? Initialization is the process of assigning the value to the elements of vector. In this article, we will learn how to initialize the vector with default value in C++.The simplest method to initialize the vector with default value is by using constructor during declaration. Letâs take a look at a sim 2 min read How to Initialize a Vector with Values between a Range in C++? In C++, vectors are dynamic containers that can resize automatically when elements are inserted or deleted during the runtime. In this article, we will learn how to initialize a vector with a range of values in C++. Example: Input: start = 1, end =5 Output: myVector: {1, 5, 3, 4} Initialize a Vector 2 min read Different Ways to Initialize an Set in C++ Initializing a set means assigning some initial values to the elements of the set container. In this article, we will learn different methods to initialize an std::set in C++.Table of ContentUsing Initializer ListOne by One InitializationFrom Another std::setFrom Another STL Container or ArrayUsing 3 min read How to Initialize 2D Vector in C++? Initializing a 2D vector refers to the process of assigning initial values to the elements of a 2D vector. In this article, we will learn different methods to initialize a 2D vector in C++.The simplest way to initialize a 2D vector is by passing the elements inside an initializer list. This method i 3 min read Like