Which data structure sorts the elements on insertion in C++ STL?

Article by: Manish Methani

Last Updated: September 7, 2021 at 10:04am IST
4 min 38 sec

In the world of C++ Standard Template Library (STL), there are various data structures at your disposal for managing collections of data. Each data structure has its unique characteristics and purposes. In this article, we'll explore a specific type of data structure that sorts its elements automatically during insertion. We'll delve into how it works, provide an example code, and address common questions related to this concept.

Table of Contents:

1. What is a Data Structure in C++ STL?

A data structure is a container that holds and organizes data efficiently. C++ STL provides a rich collection of data structures, each designed for specific use cases. One particular data structure stands out for sorting elements during insertion: std::set.

2. Sorting on Insertion: std::set

std::set is an associative container in C++ STL that automatically sorts its elements. It ensures that elements are in a specific order, following a defined sorting criterion. The elements in a std::set are unique, meaning there are no duplicate values.

3. Example Code: Sorting Elements with std::set

Let's dive into an example to see how std::set sorts elements during insertion:

#include <iostream>
#include <set>

int main() {
    std::set sortedSet;

    // Insert elements into the set

    // Display the sorted elements
    for (const auto& element : sortedSet) {
        std::cout << element << " ";

    return 0;


1 2 5 8 

In this example, we create a std::set called sortedSet and insert elements into it. When we iterate over the set, you'll notice that the elements are displayed in ascending order.


1. How does std::set ensure sorted elements?


`std::set` ensures sorted elements by utilizing a balanced binary search tree, often a Red-Black Tree. During each insertion operation, the container maintains a balanced and sorted structure to guarantee elements are in the desired order.

2. Can I customize the sorting criteria in std::set?


Yes, you can customize the sorting criteria in `std::set`. By providing a custom comparison function when defining the `std::set` container, you can control how elements are sorted based on your specific criteria.

3. What is the time complexity of insertion and retrieval in std::set?


The average time complexity of insertion and retrieval operations in `std::set` is O(log n). This makes it an efficient choice for maintaining sorted elements while offering fast access times for data retrieval.

4. When should I choose std::set for sorted elements?


You should choose `std::set` when you need a container that automatically sorts elements during insertion. It is particularly useful when you require a collection of unique elements that should be maintained in sorted order, making it an ideal choice for various programming scenarios.


std::set is a valuable tool in the C++ STL for managing sorted elements. Its automatic sorting during insertion and efficient retrieval make it a great choice for various applications. Understanding how data structures like std::set work can help you make informed decisions when choosing the right container for your specific programming needs.

Simplified Coding for Beginners.

Download the Codzify
Mobile App

Mobile App Development, Web App Development, Programming Languages, Latest Tech News & lot more.

Codzify Mobile App

A self-paced learning Courses Created by an Engineer
For Engineers.


The Complete Angular Course

Instructor: Manish Methani

Explore Curriculum

C Programming for Absolute Beginners

Instructor: Manish Methani

Start Watching

Flutter Mobile App Development Course

Instructor: Manish Methani

Explore Curriculum

Learn HTML, CSS & Bootstrap

Instructor: Manish Methani

Start Watching

Test your skills with these expert-led curated
Mock Tests.

C Programming Test

Test your C Programming skills with this comprehensive mock test on C Programming.

Take Test

Flutter Test

Solve most asked Interview Questions on Flutter and Test your foundational skills in flutter.

Take Test

GATE(CSE) Operating Systems

Solve most asked GATE Questions in Operating Systems and test your Gate Score.

Take Test


This is a mock test designed to help you assess your knowledge and skills in HTML and CSS.

Take Test

(GATE CSE) Data Structures & Algorithms Test

Solve most asked GATE Questions in Data Structures and Algorithms and test your Gate Score.

Take Test
include_once 'codzify-footer.php'; ?>