Set Constructor - C++ Programming Tutorial
C++ Course / STL Set & Map / Set Constructor

Set Constructor

BLUF: Mastering Set Constructor is a critical step in becoming a proficient C++ developer. This lesson provides a deep dive into the syntax, performance considerations, and real-world applications of this concept.
Key Performance Insight: Set Constructor

C++ is renowned for its efficiency. Learn how Set Constructor enables low-level control and high-performance computing in the tutorial below.

There are following five uses of set constructor:

  • default constructor: This is used to construct an empty set container with zero elements.
  • range constructor: This is used to construct a container with the contents of range [first, last).
  • copy constructor: This is used to construct a set with a copy of the elements of existing container.
  • move constructor: This is used to construct the container with the elements of other with the use of move semantics.
  • initializer list constructor: This is used to construct the set with the contents of the initializer list.
  • Syntax

    Default constructor

Example

explicit set (const key_compare& comp = key_compare(),
              const allocator_type& alloc = allocator_type());	//until C++ 11

explicit set (const key_compare& comp = key_compare(),
              const allocator_type& alloc = allocator_type());
explicit set (const allocator_type& alloc);			//since C++ 11

range constructor

Example

template <class InputIterator>
  set (InputIterator first, InputIterator last,
       const key_compare& comp = key_compare(),
       const allocator_type& alloc = allocator_type());		//until C++ 11

template <class InputIterator>
  set (InputIterator first, InputIterator last,
       const key_compare& comp = key_compare(),
       const allocator_type& = allocator_type());			//since C++ 11

copy constructor

Example

set (const set& x);						//until C++ 11
	
set (const set& x);
set (const set& x, const allocator_type& alloc);			//since C++ 11

move constructor

Example

set (set&& x);
set (set&& x, const allocator_type& alloc);			//since C++ 11

initializer list constructor

Example

set (initializer_list<value_type> il,
     const key_compare& comp = key_compare(),
     const allocator_type& alloc = allocator_type());		//since C++ 11

Parameter

A comparison function object is utilized with two key arguments to determine if the first argument precedes the second argument, returning true in that case. By default, it employs the less<key_type> predicate.

The alloc object is utilized for handling all memory allocations within this container.

Input iterator points to the initial position within a specified range.

last: Input iterator pointing to the final position within a specified range.

x : Another set object of the same type.

An initializer list object is required from which the elements will be duplicated.

Return value

Constructor never returns any value.

Complexity

For blank constructors and transfer constructors, the complexity will remain constant.

In all other scenarios, the time complexity will increase linearly based on the gap between the iterators when the elements are in sorted order.

Iterator validity

Invalidate all pointers, iterators, and references associated with x when the elements of the set container are transferred in the move constructor.

Data Races

All copied elements are accessed.

Exception Safety

No effects in case an exception is thrown.

Example 1

Let's explore a basic illustration for the default constructor:

Example

#include <iostream>
#include <set>

using namespace std;

int main(void) {
   // Default constructor
   set<char> s;
  
   int size = s.size(); 

   cout << "Size of set s = " << size;
   return 0;
}

Output:

Output

Size of set = 0

In the previously mentioned example, 's' represents an empty set, resulting in a size of 0.

Example 2

Let's see a simple example for range constructor:

Example

#include <iostream>
#include <set>

using namespace std;

int main(void) {
   int evens[] = {2,4,6,8,10};
  
   // Range Constructor
   set<int> myset (evens, evens+5);  

   cout << "Size of set container myset is : " << myset.size();
   return 0;
}

Output:

Output

Size of set container myset is: 5

In the example provided, the set named myset is formed using the elements from the evens set.

Example 3

Let's see a simple example for copy constructor:

Example

#include <iostream>
#include <set>

using namespace std;

int main(void) {
   //Default Constructor
   std::set<int> s1;
   s1.insert(5);
   s1.insert(10);

   cout << "Size of set container s1 is : " << s1.size();
  
   // Copy constructor
   set<int> s2(s1);
   cout << "\nSize of new set container s2 is : " << s2.size();
   return 0;
}

Output:

Output

Size of set container s1 is : 2
Size of new set container s2 is : 2

In the above example, s2 is a copy of s1 set.

Example 4

Let's see a simple example for move constructor:

Example

#include <iostream>
#include <set>

using namespace std;

int main(void) {
   // Default constructor
   set<char> s1;
   s1.insert('x');
   s1.insert('y');

   cout << "Size of set container s1 is : " << s1.size();

   // Move constructor
   set<char> s2(move(s1));
   cout << "\nSize of new set container s2 is : " << s2.size();
   return 0;
}

Output:

Output

Size of set container s1 is : 2
Size of new set container s2 is : 2

In the aforementioned example, the contents of set s1 are transferred to set s2.

Example 5

Let's examine a basic illustration for the initializer list constructor:

Example

#include <iostream>
#include <set>
#include <string>

using namespace std;

int main() {
   // Initializer list constructor
   set<string> fruit {
      "orange", "apple", "mango", "peach", "grape"
   };

   cout << "Size of set container fruit is : " << fruit.size();
   return 0;
}

Output:

Output

Size of set container fruit is : 5

The aforementioned example constructs a collection of fruits with strings as keys and sets it up with an initializer list.

Input Required

This code uses input(). Please provide values below:

Logic Practice
Install Logic Practice
Add to home screen for a faster app-like experience