0Pricing
C++ Academy · Lesson

std::set

Ordered unique elements.

std::set is a free C++ Academy lesson on CoddyKit — lesson 2 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the C++ Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

What Is std::set?

std::set stores unique elements in sorted order. There are no duplicates, and lookups run in logarithmic time.

  • Keys are also the values.
  • Great for membership tests and de-duplication.
#include <iostream>
#include <set>

int main() {
    std::set<int> s{3, 1, 2, 1, 3};
    for (int x : s) std::cout << x << ' ';
    std::cout << '\n';
    return 0;
}

Inserting Values

Use insert() to add elements. Inserting a value that already exists is simply ignored.

#include <iostream>
#include <set>

int main() {
    std::set<std::string> s;
    s.insert("apple");
    s.insert("banana");
    s.insert("apple");
    std::cout << s.size() << " unique items\n";
    return 0;
}

Insert Return Value

insert() returns a pair whose .second is a bool telling you whether the insertion actually happened.

#include <iostream>
#include <set>

int main() {
    std::set<int> s{1, 2};
    auto r = s.insert(2);
    std::cout << std::boolalpha << "inserted: " << r.second << '\n';
    auto r2 = s.insert(5);
    std::cout << "inserted: " << r2.second << '\n';
    return 0;
}

Checking Membership

Use count() or C++20 contains() to test if a value is present.

#include <iostream>
#include <set>

int main() {
    std::set<int> s{10, 20, 30};
    std::cout << s.count(20) << '\n';
    std::cout << s.count(99) << '\n';
    return 0;
}

Finding Elements

find() returns an iterator to the matching element, or end() when absent.

#include <iostream>
#include <set>

int main() {
    std::set<std::string> s{"red", "green", "blue"};
    auto it = s.find("green");
    std::cout << (it != s.end() ? "found" : "missing") << '\n';
    return 0;
}

Erasing Elements

erase() removes a value by key and returns how many were removed (0 or 1).

#include <iostream>
#include <set>

int main() {
    std::set<int> s{1, 2, 3, 4};
    s.erase(3);
    for (int x : s) std::cout << x << ' ';
    std::cout << '\n';
    return 0;
}

Sorted Order

Elements are always traversed in ascending order, no matter how they were inserted.

#include <iostream>
#include <set>

int main() {
    std::set<int> s{50, 10, 40, 20, 30};
    for (int x : s) std::cout << x << ' ';
    std::cout << '\n';
    return 0;
}

De-duplicating Data

Feeding values into a set is a quick way to remove duplicates and sort at the same time.

#include <iostream>
#include <set>
#include <vector>

int main() {
    std::vector<int> v{4, 2, 4, 1, 2, 3, 1};
    std::set<int> unique(v.begin(), v.end());
    for (int x : unique) std::cout << x << ' ';
    std::cout << '\n';
    return 0;
}

lower_bound and upper_bound

Because the set is ordered, lower_bound() and upper_bound() let you find ranges efficiently.

#include <iostream>
#include <set>

int main() {
    std::set<int> s{10, 20, 30, 40, 50};
    auto lo = s.lower_bound(20);
    auto hi = s.upper_bound(40);
    for (auto it = lo; it != hi; ++it) std::cout << *it << ' ';
    std::cout << '\n';
    return 0;
}

Size and Clear

size() reports the element count, empty() tests for none, and clear() removes everything.

#include <iostream>
#include <set>

int main() {
    std::set<int> s{1, 2, 3};
    std::cout << s.size() << '\n';
    s.clear();
    std::cout << std::boolalpha << s.empty() << '\n';
    return 0;
}

Range Insertion

You can insert a whole range from another container, automatically dropping duplicates and sorting.

#include <iostream>
#include <set>

int main() {
    std::set<int> s{1, 2, 3};
    int more[] = {3, 4, 5};
    s.insert(more, more + 3);
    for (int x : s) std::cout << x << ' ';
    std::cout << '\n';
    return 0;
}

Quick Check

Test your understanding of std::set behavior.

Recap

You learned that std::set:

  • holds unique, sorted values
  • silently ignores duplicate inserts
  • supports find, count, erase, and ordered lower_bound/upper_bound queries

Next, you'll meet multimap and multiset which allow duplicates.

Frequently asked questions

Is the “std::set” lesson free?

Yes — the full text of “std::set” is free to read here on the web, and the C++ Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the C++ Academy course, upgrade to CoddyKit PRO.

What will I learn in “std::set”?

Ordered unique elements. You practise C++ Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start C++ Academy?

No prior experience is required. C++ Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 2 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “std::set” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this C++ Academy lesson?

Yes. Every C++ Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. std::map
  2. std::set
  3. multimap and multiset
  4. Custom Comparators
← Back to C++ Academy