Our June 2021 cohorts are filling up quickly. Join our free webinar to Uplevel your career
About usWhy usInstructorsReviewsCostFAQContactBlogRegister for Webinar

Attend our Free Webinar on How to Nail Your Next Technical Interview

WEBINAR +LIVE Q&A

How To Nail Your Next Tech Interview

Hosted By
Ryan Valles
Founder, Interview Kickstart
Our tried & tested strategy for cracking interviews
How FAANG hiring process works
The 4 areas you must prepare for
How you can accelerate your learnings

C++ STL Container Fundamentals: Set

The Standard Template Library (STL) in C++ is a powerful software library that’s a set of C++ template classes. It provides built-in algorithms, functions, iterators, and containers. This article focuses on the C++ STL container set.

STL containers are objects that can store multiple elements, manage any storage required for the elements, and offer member functions we can use to access them. A container may allow elements of either the same type or different types to be stored in it. Depending on this, and on whether it is unordered, the containers are divided into three types:

  • Sequence Containers: deque, arrays, vector, list, and forward_list
  • Associative Containers: set, multiset, map, and multimap
  • Unordered Associative Containers: unordered_set, unordered_multiset, unordered_map, and unordered_multimap

There are also Container Adapters: queue, priority_queue, and stack that are a subset of containers. These container adapters offer a different interface for sequential containers. 

To help you harness the power of STL and be a more efficient developer, we’re doing a series on C++ STL container fundamentals. This article focuses on the C++ STL container set (check out the learn page for more).

Having trained over 11,000 software engineers, we know what it takes to crack the toughest tech interviews. Our alums consistently land offers from FAANG+ companies. The highest ever offer received by an IK alum is a whopping $1.267 Million!

At IK, you get the unique opportunity to learn from expert instructors who are hiring managers and tech leads at Google, Facebook, Apple, and other top Silicon Valley tech companies.

Want to nail your next tech interview? Sign up for our FREE Webinar.

In this article, we’ll cover:

  • C++ STL Container Fundamentals: Set
  • Use of the C++ STL Container Set
  • Methods of set in C++ STL
  • How to Use the C++ STL Container Set
  • FAQs on C++ STL Container Fundamentals: Set

C++ STL Container Fundamentals: Set

Sets are associative containers implemented using binary search trees that contain unique elements stored in a specific order. By default, the elements get popped out in ascending order. However, the order can also be customized using a custom comparator passed to a set in C++ STL. 

Once a value is stored in a set, we can’t modify it within the set. We have to remove that value and add the revised version of the element value. 

Use of the C++ STL Container Set

Sets are widely used to:

  • Store unique, non-repeating, ordered values like IDs at hotels, work, hospitals, etc.
  • Test whether an item belongs to a set of values
  • Perform operations on multiple values at once using methods like union, intersect, etc.

Methods of Set in C++ STL

Here are the several methods associated with set in STL:

Let us now see what some of the most commonly used set methods do and how to use them in the next section via an example.

How to Use the C++ STL Container set

Here, we take a look at how you can use set as a C++ STL container for a smoother coding experience:

Code: C++ STL set Example


// Set in STL C++
#include 
#include 
#include 

using namespace std;

void printSetDefault(set < int > setToPrint) {
    set < int > ::iterator iteratorI;
    for (iteratorI = setToPrint.begin(); iteratorI != setToPrint.end(); iteratorI++) {
        cout << * iteratorI << " ";
    }
    cout << "\n";
}

void printSet(set < int, greater < int > > setToPrint) {
    set < int, greater < int > > ::iterator iteratorI;
    for (iteratorI = setToPrint.begin(); iteratorI != setToPrint.end(); iteratorI++) {
        cout << * iteratorI << " ";
    }
    cout << "\n";
}

int main() {
    // Showing through simple default example that set stores in ascending order by default
    set < int > setExampleDefault;
    setExampleDefault.insert(1);
    setExampleDefault.insert(6);
    setExampleDefault.insert(9);
    setExampleDefault.insert(4);
    setExampleDefault.insert(1);

    // Printing the set setExampleDefault
    cout << "The set setExampleDefault is: \n";
    printSetDefault(setExampleDefault);

    // We can store the items in decending order using the following declaration
    set < int, greater < int > > setExample;

    // Using the set setExample, let us look at some more functions set offers us. (Valid when using default set ordering too.)
    // Adding elements in no particular order
    setExample.insert(2);
    setExample.insert(3);
    setExample.insert(5);
    setExample.insert(8);
    setExample.insert(7);
    setExample.insert(1);

    // Despite adding the element 3 four more times, it will still show up only once in the set
    setExample.insert(3);
    setExample.insert(3);
    setExample.insert(3);
    setExample.insert(3);

    // Printing the set setExample
    cout << "The set setExample is: \n";
    printSet(setExample);

    // Assigning all the elements from setExample to setExample2
    set < int, greater < int > > setExample2(setExample.begin(), setExample.end());

    // Printing all the elements of the set setExample2
    cout << "The set setExample2 after assigning all the elements from setExample is: \n";
    printSet(setExample2);

    // Deleting the element 3 in setExample2 then printing it
    cout << "setExample2 after deleting the element 3: \n";
    setExample2.erase(3);
    printSet(setExample2);

    // Deleting all the elements up to (not including) 5 in setExample2 then printing it
    cout << "setExample2 after deleting all the elements up to (not including) 5: \n";
    setExample2.erase(setExample2.begin(), setExample2.find(5));
    printSet(setExample2);

    // lowerbound(item) returns an iterator to the first element that is either equivalent to ‘item’ or surely will not come before the ‘item’ in the set.
    // upperbound(item) returns an iterator to the first element that will come after the ‘item’ in the set.
    // The lower and upper bound for both sets
    cout << "setExample.lower_bound(5) gives: " << * setExample.lower_bound(5) << "\n";
    cout << "setExample.upper_bound(5) gives: " << * setExample.upper_bound(5) << "\n";
    cout << "setExample2.lower_bound(5) gives: " << * setExample2.lower_bound(5) << "\n";
    cout << "setExample2.upper_bound(5) gives: " << * setExample2.upper_bound(5) << "\n";

    return 0;
}

Output


The set setExampleDefault is: 
1 4 6 9 
The set setExample is: 
8 7 5 3 2 1 
The set setExample2 after assigning all the elements from setExample is: 
8 7 5 3 2 1 
setExample2 after deleting the element 3: 
8 7 5 2 1 
setExample2 after deleting all the elements up to (not including) 5: 
5 2 1 
setExample.lower_bound(5) gives: 5
setExample.upper_bound(5) gives: 3
setExample2.lower_bound(5) gives: 5
setExample2.upper_bound(5) gives: 2

FAQs on C++ STL Container Fundamentals: set

Q1. What is a set in STL?

Set is a C++ STL container used to store the unique elements, and all the elements are stored in a sorted manner. 

Q2. Is the set ordered in C++ STL?

Yes, in C++, set order is guaranteed in STL.

Q3. Which is better set or unordered set?

In a set, we can traverse elements in a specific sorted order and find the predecessor or successor of an element. But in an unordered set, we can’t traverse elements in any specific sorted order and can’t find any predecessor or successor.

Q4. Is unordered_set faster than set in C++ STL?

Yes. Unordered set containers are faster than set containers in C++ STL.

Q5. Can we add duplicate elements to a set?

Even if we add duplicate elements to a set, only unique values will be stored in it. If an instance of the element added is already present in the set, the set doesn’t add it again.

Ready to Nail Your Next Coding Interview?

Whether you’re a coding engineer gunning for a software developer or software engineer role, a tech lead, or targeting management positions at top companies, IK offers courses specifically designed for your needs to help you with your technical interview preparation!

If you’re looking for guidance and help with getting started, sign up for our FREE webinar. As pioneers in technical interview preparation, we have trained thousands of software engineers to crack the most challenging coding interviews and land jobs at their dream companies, such as Google, Facebook, Apple, Netflix, Amazon, and more!

Sign up now!

Attend our Free Webinar on How to Nail Your Next Technical Interview

WEBINAR +LIVE Q&A

How To Nail Your Next Tech Interview

Hosted By
Ryan Valles
Founder, Interview Kickstart
Our tried & tested strategy for cracking interviews
How FAANG hiring process works
The 4 areas you must prepare for
How you can accelerate your learnings

Recommended Posts

All Posts