In this article, we will discuss how to create a set in C++ and initialize it with default values.

A set is an associative container provided by the Standard Template Library (STL) in C++. It stores only unique elements and maintains them in sorted order. It supports fast searching with a time complexity of O(log n).

Header file required for the set in C++

To use the set class in C++, we must first include the set header file. Like this,

#include <set>

This header file contains the set class declaration.

Set Syntax and Template Parameters

The general syntax to create a set object is as follows.,

std::set<Key, Compare, Allocator> setObjectName;

When we create a set object, we can provide three template parameters. Out of these three, the last two are optional. Let’s look into these parameters:

  1. Key: This determines the data type of the elements that will be stored in the set.
  2. Compare: This is a binary predicate that defines the order of the elements. The set uses this for internal comparisons between elements. By default, this is set to std::less<Key>, meaning the set will store elements in ascending order.
  3. Allocator: This specifies the allocator used to acquire/release memory and to construct/destroy the elements. By default, it is set to std::allocator<Key>.

Most often, we’ll only specify the Key parameter (the data type of the elements). For the other two, Compare and Allocator, we usually rely on their default settings.

Creating and Initializing a Set with default values

To create a set of integers, we specify int as the template parameter when creating the set object. Additionally, we can initialize the set using an initializer list, as follows:

std::set<int> setObj {11, 3, 21, 19, 2, 1};

A set always stores its elements in sorted order, with the default being ascending order. Thus, even if we inserted random integer values into the setObj in the previous example, they would be stored in ascending order. The contents of the set would appear as follows:

1, 2, 3, 11, 19, 21

A set in C++ is designed to store only unique elements. Therefore, if we attempt to initialize a set object with duplicate values, it will disregard the duplicates and only store the unique values. In the following example, we’ll initialize a set object using an initializer list that contains a few duplicate values:

std::set<int> setObj2 {11, 3, 21, 19, 2, 11, 31, 21, 1};

Contents of the Set will be,

1, 2, 3, 11, 19, 21, 31

The set will store only the unique elements from the provided initializer list, and, importantly, in sorted order. Any duplicate values will be skipped.
In the preceding code, we attempted to initialize setObj2 with the duplicate values 11 and 21. Yet, when we examine the set’s contents, it’s evident that each number is stored uniquely and in ascending sequence.

The complete example is as follows,

#include <iostream>
#include <set>

int main()
    // Creating a Set & initializing it with default values
    std::set<int> setObj {11, 3, 21, 19, 2, 11, 31, 21, 1};

    for (int a: setObj)
        std::cout << a << ", ";
    std::cout << std::endl;

    return 0;


1, 2, 3, 11, 19, 21, 31, 

We tried to initialize the set with duplicate values, but it stored only unique elements and arranged them in sorted order. We used the range-based for loop to iterate over the set elements. The Range-based For Loop was introduced in C++11, allowing for easier iteration over collections. It offers a simpler way to iterate over all the elements of a container. We will discuss it further in a later section of this series.


In this article, we learned how to create a set in C++ and initialize it with default values.

Ritika Ohri

Hi, I am Ritika Ohri, founder of this blog. I craft comprehensive programming tutorials and also manage a YouTube channel. You can also connect with me on Linkedin.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.