Lesson 5

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!

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:

C++`1#include <set>`

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

C++`1#include <set> 2 3std::set<int> mySet;`

This declares a set named `mySet`

for integers.

Add elements using the `insert`

function:

C++`1#include <set> 2#include <iostream> 3 4int main() { 5 std::set<int> mySet; 6 7 mySet.insert(10); 8 mySet.insert(20); 9 mySet.insert(30); 10 11 return 0; 12}`

If you try to insert `10`

again, it won't be added as it’s a duplicate.

C++`1mySet.insert(10); // Duplicate, won't be added`

However, it won't cause any errors.

Remove an item with the `erase`

function:

C++`1#include <set> 2#include <iostream> 3 4int main() { 5 std::set<int> mySet; 6 7 mySet.insert(10); 8 mySet.insert(20); 9 mySet.insert(30); 10 11 // Remove element 20 12 mySet.erase(20); 13 14 return 0; 15}`

Use `find`

to check if an element is in the set. Find is a complex function that returns a thing called `iterator`

pointing to the set's element. We will cover the details of it in the "Intermediate Programming in C++" path. By now, let's just say that if the `find`

method doesn't find the element, it returns a thing which is equal to `set.end()`

, so here is how we can make the comparison:

C++`1#include <set> 2#include <iostream> 3 4int main() { 5 std::set<int> mySet; 6 7 mySet.insert(10); 8 mySet.insert(20); 9 mySet.insert(30); 10 11 // Check if 10 is in the set 12 if (mySet.find(10) != mySet.end()) { 13 std::cout << "10 is in the set" << std::endl; // Output: 10 is in the set 14 } 15 16 return 0; 17}`

It works, but seems a bit tricky and hard to remember, right? Let's try out another approach!

Alternatively, use the `count`

function:

C++`1#include <set> 2#include <iostream> 3 4int main() { 5 std::set<int> mySet; 6 7 mySet.insert(10); 8 mySet.insert(20); 9 mySet.insert(30); 10 11 // Check if 20 is in the set 12 if (mySet.count(20) > 0) { 13 std::cout << "20 is in the set" << std::endl; // Output: 20 is in the set 14 } 15 16 return 0; 17}`

The set can only have one instance of each element, so `.count`

will always return either `0`

or `1`

.

Finally, we can iterate the set using a range-based for loop:

C++`1#include <set> 2#include <iostream> 3 4int main() { 5 std::set<int> mySet; 6 7 mySet.insert(20); 8 mySet.insert(10); 9 mySet.insert(30); 10 11 // Print all elements 12 for (int item : mySet) { 13 std::cout << item << " "; // Output: 10 20 30 14 } 15 16 return 0; 17}`

This code prints: `10 20 30 `

. Note that the set always stores elements in the ascending order. The order of adding elements to the set doesn't matter.

Great job! Here’s what we learned:

- Sets store unique elements in a specific order.
- Include the
`<set>`

header to use sets. - Define, add, remove, and check elements in a set using
`insert`

,`erase`

,`find`

, and`count`

. - Use iterators to loop through set elements.

Sets are great for handling collections of unique items efficiently.

Now, let's practice! You'll define your own sets, add and remove elements, and check for item existence. Ready? Let's start practicing on CodeSignal!