1. Introduction to C++: A Comprehensive Guide with Code Examples - 2023 2. Mastering Data Types in C++: A Comprehensive Guide with Codes and Examples (2023) 3. Learn About Variables and Types of Variables in C++ | Codzify.com 4. Control Statements in C++: A Comprehensive Guide for 2023 5. C++ Tutorial: Understanding Switch Statements with Codes and Examples in 2023 6. Understanding Memory Allocation and Pointers in C++: A Beginners Guide 7. Functions in C++ 8. Call by value and Call by Reference in C++ in depth 9. Array in C++ 10. 2d arrays in C++ 11. Classes and Objects in C++ 12. Static Functions in C++ 13. Constructors and Destructors in C++ - A Complete Guide with Examples 14. Mastering Copy Constructor in C++ - Shallow vs Deep Copy with Examples | Codzify 15. Understanding Friend Functions in C++ Made Simple! 16. Inline Functions in C++ 17. this Pointer in C++ 18. Mastering Inheritance in C++: Types and Examples Explained 19. Types of Inheritance in C++ 20. Polymorphism in C++ Explained with Codes and Examples in 2023 21. Templates in C++ 22. Getting the Value of a MultiMap in C++: Step-by-Step Guide with Examples 23. Multimap Find and Replace Operator in C++: Step-by-Step Guide - Codzify Topics 24. Exploring the Next_Permutation Algorithm without STL in C++ - Codzify Topics 25. C++ - The Difference Between Map and HashMap in STL 26. Updating Values in a std::multimap in C++ - Codzify Topics 27. Which data structure sorts the elements on insertion in C++ STL? 28. Can we implement Red Black Tree in c++ by STL containers? 29. How to Dynamically Declare an Array of Objects with a Constructor in C++ - A Step-by-Step Guide 30. What is the difference between a pointer and an object in C++? 31. Mastering Red-Black Trees with STLs Internal Implementation: A Step-by-Step Guide

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.

Watch Video Tutorials at Codzify YouTube Channel:

Codzify Logo

Terms and Conditions    Cookie Policy   Refund Policy   Adsense Disclaimer

Contact: teamcodzify@gmail.com