Register for our webinar

How to Nail your next Technical Interview

1 hour
Loading...
1
Enter details
2
Select webinar slot
*Invalid Name
*Invalid Name
By sharing your contact details, you agree to our privacy policy.
Step 1
Step 2
Congratulations!
You have registered for our webinar
check-mark
Oops! Something went wrong while submitting the form.
1
Enter details
2
Select webinar slot
*All webinar slots are in the Asia/Kolkata timezone
Step 1
Step 2
check-mark
Confirmed
You are scheduled with Interview Kickstart.
Redirecting...
Oops! Something went wrong while submitting the form.
close-icon
Iks white logo

You may be missing out on a 66.5% salary hike*

Nick Camilleri

Head of Career Skills Development & Coaching
*Based on past data of successful IK students
Iks white logo
Help us know you better!

How many years of coding experience do you have?

Thank you! Your submission has been received!
Oops! Something went wrong while submitting the form.
Iks white logo

FREE course on 'Sorting Algorithms' by Omkar Deshpande (Stanford PhD, Head of Curriculum, IK)

Thank you! Please check your inbox for the course details.
Oops! Something went wrong while submitting the form.
Our June 2021 cohorts are filling up quickly. Join our free webinar to Uplevel your career
close
closeAbout usWhy usInstructorsReviewsCostFAQContactBlogRegister for Webinar

C++ STL Container Fundamentals: Set

Last updated by Dipen Dadhaniya on Apr 01, 2024 at 01:04 PM | Reading time: 5 minutes

The fast well prepared banner

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

WEBINAR +LIVE Q&A

How To Nail Your Next Tech Interview

C++ STL Container Fundamentals: Set
Hosted By
Ryan Valles
Founder, Interview Kickstart
strategy
Our tried & tested strategy for cracking interviews
prepare list
How FAANG hiring process works
hiring process
The 4 areas you must prepare for
hiring managers
How you can accelerate your learnings

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!

Last updated on: 
April 1, 2024
Author

Dipen Dadhaniya

Engineering Manager at Interview Kickstart

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

Register for our webinar

How to Nail your next Technical Interview

1
Enter details
2
Select webinar slot
By sharing your contact details, you agree to our privacy policy.
Step 1
Step 2
Congratulations!
You have registered for our webinar
check-mark
Oops! Something went wrong while submitting the form.
1
Enter details
2
Select webinar slot
Step 1
Step 2
check-mark
Confirmed
You are scheduled with Interview Kickstart.
Redirecting...
Oops! Something went wrong while submitting the form.

C++ STL Container Fundamentals: Set

Worried About Failing Tech Interviews?

Attend our webinar on
"How to nail your next tech interview" and learn

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