Lesson Introduction

Hey there! Today, we're exploring sets in C++ — a useful way to handle collections of unique items. By the end of this lesson, you’ll know how to define sets, add and remove items, check for specific items, and iterate through all the items in a set. Let's dive in!

Understanding Sets in C++

A set in C++ is a container holding unique elements, just like a stamp collection without duplicates. Sets are perfect for tracking unique items without worrying about duplicates — like a class attendance list ensuring no name repeats.

To use sets in C++, include the set library with this line:

Defining a Set

To create an empty set that stores integers, define it like this:

This declares a set named mySet for integers.

Adding Elements

Add elements using the insert function:

If you try to insert 10 again, it won't be added as it’s a duplicate.

Sign up
Join the 1M+ learners on CodeSignal
Be a part of our community of 1M+ users who develop and demonstrate their skills on CodeSignal