Skip to main content

Cplusplus Sets

C++ sets can seem intimidating at first, but they are powerful tools for organizing data. In simplest terms, a set is a collection of unique elements. If you've ever felt frustrated managing duplicates in your program, sets can solve that problem for you.

In this article, I'll guide you through the essentials of C++ sets. We’ll cover how they work, common use cases, and see examples of their implementation.


What Is a Set in C++?

A set is a part of C++'s Standard Template Library (STL). Think of it like a container that automatically ensures all its elements are unique. Unlike arrays or vectors, sets don’t allow duplicates. If you try adding an element that already exists, it simply won’t be added.

Additionally, sets store their elements in sorted order. This means you don’t need to manually sort the data—it's handled for you.

Sets are especially handy if you need fast lookups, insertions, or deletions. They work behind the scenes using something called a binary search tree, which keeps operations efficient.


Why Use C++ Sets?

You might wonder, why sets? Aren’t arrays, lists, or vectors enough? While those structures are great for many tasks, sets excel when you:

  • Need to ensure all elements are unique.
  • Want to quickly check if an element exists.
  • Automatically maintain sorted order.

Let’s say you’re creating a program to keep track of user IDs. Using a vector allows duplicates, which could cause problems. With a set, you’re guaranteed each ID will be unique. No extra effort is required to check or filter for duplicates manually.


How to Create a Set in C++

Creating a set is simple. Start by including the <set> header, then declare your set just like any other container. Here's a basic example:

#include <iostream>
#include <set>

int main() {
    std::set<int> numbers; // Create an empty set of integers

    numbers.insert(10); // Add elements
    numbers.insert(20);
    numbers.insert(10); // Duplicate, won't be added

    std::cout << "Set contains: ";
    for (int num : numbers) {
        std::cout << num << " ";
    }

    return 0;
}

Output:
Set contains: 10 20

Notice how the duplicate 10 wasn’t added, and the output is sorted.


Key Operations in Sets

Working with sets involves a handful of important operations. Let’s break them down.

1. Inserting Elements

To add an element, use the insert() method. It won’t add duplicates:

std::set<string> fruits;
fruits.insert("apple");
fruits.insert("banana");
fruits.insert("apple"); // Duplicate

for (const auto &fruit : fruits) {
    std::cout << fruit << " ";
}

Output:
apple banana

2. Removing Elements

Use the erase() method to remove elements from a set:

fruits.erase("apple"); // Remove "apple"

If the element doesn’t exist, erase() simply does nothing.

3. Finding Elements

To check if an element exists, use the find() method. It returns an iterator pointing to the element if found, or end() if not:

if (fruits.find("banana") != fruits.end()) {
    std::cout << "Banana is in the set!" << std::endl;
}

4. Size and Emptiness

Use size() to check how many elements are in the set, and empty() to see if it’s empty:

std::cout << "Size: " << fruits.size() << std::endl;
if (fruits.empty()) {
    std::cout << "The set is empty." << std::endl;
}

Example: Managing Unique Words in a Sentence

Here’s a practical example: removing duplicate words from user input.

#include <iostream>
#include <set>
#include <string>
#include <sstream>

int main() {
    std::set<std::string> uniqueWords;
    std::string sentence;
    
    std::cout << "Enter a sentence: ";
    std::getline(std::cin, sentence);

    std::stringstream ss(sentence);
    std::string word;

    while (ss >> word) {
        uniqueWords.insert(word); // Add words to the set
    }

    std::cout << "Unique words: ";
    for (const auto &w : uniqueWords) {
        std::cout << w << " ";
    }

    return 0;
}

Example: Counting Unique Numbers

Imagine you’re processing data and need to count distinct integers in a list.

#include <iostream>
#include <set>

int main() {
    std::set<int> numbers = {1, 5, 3, 7, 5, 3, 7};

    std::cout << "Unique numbers: ";
    for (const auto &num : numbers) {
        std::cout << num << " ";
    }

    std::cout << "\nCount: " << numbers.size() << std::endl;

    return 0;
}

Example: Comparing Multisets and Sets

Multisets are another container in C++ that allow duplicates. Here’s a comparison:

#include <iostream>
#include <set>
#include <map>

int main() {
    std::set<int> uniqueSet = {1, 2, 2, 3};
    std::multiset<int> multiSet = {1, 2, 2, 3};

    std::cout << "Set: ";
    for (int n : uniqueSet) std::cout << n << " "; // Outputs: 1 2 3

    std::cout << "\nMultiset: ";
    for (int n : multiSet) std::cout << n << " "; // Outputs: 1 2 2 3

    return 0;
}

Set vs. Unordered Set

Regular sets store elements in sorted order. But if you don’t care about sorting, you can use an unordered set for better performance. Unordered sets are faster for lookups but don’t maintain any order.

#include <iostream>
#include <unordered_set>

int main() {
    std::unordered_set<int> numbers = {3, 1, 2, 4};

    std::cout << "Unordered set values: ";
    for (int n : numbers) {
        std::cout << n << " ";
    }

    return 0;
}

The output order varies—elements aren’t sorted.


Conclusion

C++ sets offer a reliable way to store unique elements while keeping them sorted. Whether you’re managing user IDs, filtering duplicate data, or quickly finding values, sets are an invaluable tool.

Understanding their basic operations like insertion, deletion, and lookup is key to using them effectively. With a few practical examples in your toolkit, you’re well on your way to mastering this STL container. Ready to make your code simpler and more efficient? Give sets a try in your next project!

Popular posts from this blog

C++ vcpkg Manifest Mode + CMake

 If you've ever tried to install a C++ library and felt like you were assembling furniture without instructions, this article is for you. We're going to talk about vcpkg manifest mode and how it works with CMake , and I'm going to explain it like you're five years old (in a good way — no judgment here). First, Let's Talk About the Problem In most programming languages, adding a library is easy. Python has pip install requests . JavaScript has npm install express . You type one command, and boom, the library shows up in your project. C++ never really had that. For decades, if you wanted to use a library like fmt or nlohmann/json , you had to: Download the source code yourself Figure out how to compile it Tell your compiler where to find the headers Tell your linker where to find the compiled binaries Cry a little vcpkg is Microsoft's answer to this mess. It's a package manager for C++ — like pip or npm , but for C++ libraries. And manifest mode...

How to Set Up a Linux Web Server and Host an HTML Page Easily

Setting up a web server on Linux means spending a fair amount of time in the terminal — Linux leans heavily on the command line rather than clicking through menus, so you'll be typing out instructions more often than not.  If you're new to this, it can feel a little intimidating at first, but the good news is you don't need to become a Linux wizard overnight. A handful of core commands will get you surprisingly far. A few you'll lean on constantly: cd — move between directories ls — see what's in the current directory mkdir — create a new folder nano or vim — edit files right there in the terminal sudo — run something with administrator privileges Get comfortable with these and you'll be able to navigate around, tweak configuration files, and install software without much trouble. You don't need to memorize everything — you just need to be confident enough to follow along with clear instructions, which is exactly what this guide aims to give you....

Linux Network Troubleshooting

If you've spent any time as a sysadmin — or honestly, just as someone who's had to fix their own home network at 11pm — you know that connectivity issues are one of the most common headaches out there. The good news is that a handful of core tools and a methodical approach can take you from "why isn't this working" to a root cause pretty quickly.  This guide walks through the essentials: configuring interfaces, managing routes, and diagnosing problems when things go sideways. Configuring Network Interfaces Your network interfaces are the actual bridge between your machine and the outside world, so getting them configured correctly is step one for any kind of reliable connectivity. Doing It Manually ifconfig is the old-school, tried-and-true tool for this on Unix-like systems. To see everything currently configured, run: ifconfig -a If you need to manually set up a specific interface — assigning an IP, a netmask, and bringing it online — it looks like this: ifconf...