{}const=>[]async()letfn</>var
Development

🧰 STL: hidden features of the C++ standard library

STL is not just vector and map. We analyze useful but rarely used tools like transform, accumulate, variant, and ranges.

К

Kodik

Author

2 min read

The C++ standard library is a powerful tool that is often underestimated. Everyone knows about vector, map and string, but STL (Standard Template Library) is not only about basic containers.

Today Kodik will show little-known, but damn useful STL tricks, who will pump up your code, make it cleaner and shorter 💪

🔍 1. std::accumulate — compact aggregation

Instead of a manual cycle for calculating the sum of numbers:

int sum = 0;
for (int x : v) sum += x;

You can simply:

#include <numeric>
int sum = std::accumulate(v.begin(), v.end(), 0);
🔥 100,000+ students already with us

Tired of reading theory?
Time to code!

Kodik — an app where you learn to code through practice. AI mentor, interactive lessons, real projects.

🤖 AI 24/7
🎓 Certificates
💰 Free
🚀 Start learning
Joined today

🌀 2. std::transform — functional approach

Need to apply the operation to each element? Don't write a loop — use transform:

std::vector<int> result;
std::transform(v.begin(), v.end(), std::back_inserter(result), [](int x) {
    return x * 2;
});

🧩 3. std::any and std::variant — storage of any types

std::variant<int, std::string> val = "Hello";
if (std::holds_alternative<std::string>(val)) {
    std::cout << std::get<std::string>(val);
}

🧱 4. std::unordered_map and std::unordered_set — the power of hash tables

When you use the usual std::map and std::set, under the hood balanced binary tree (usually red-black tree). This means that insert, search, and delete operations take O(log n) time.

But std::unordered_map and std::unordered_set are arranged differently: they use hash tables. Due to this, they provide medium complexity of operations — O(1). This makes them much faster in most applied tasks, especially with large amounts of data.

🔥 Example:

#include <unordered_map>
#include <string>
#include <iostream>

int main() {
    std::unordered_map<std::string, int> scores;
    scores["Alice"] = 90;
    scores["Bob"] = 75;

    std::cout << scores["Alice"]; // 90
}

Unlike std::map, unordered_map does not guarantee the order of the elements. If you don't care about the order in which the data is stored, — use unordered_-versions for maximum performance.

🧠 When to use?

Need to keep order?

Use

Yes

std::map, std::set

No

std::unordered_map, std::unordered_set

🧵 5. std::ranges (starting with C++20)

#include <ranges>

auto result = v 
    | std::views::filter([](int x) { return x % 2 == 0; })
    | std::views::transform([](int x) { return x * x; });

Allows you to write chains of operations — beautifully and compactly.

🤯 Bonus: what else is there in STL

  • std::optional — a safe alternative to "magic values"

  • std::span — convenient transfer of ranges

  • std::bitset — compact work with flags

  • std::invoke — calling any functions, including member functions

If you want to learn how to use STL in real tasks, download application Code. There you will find:

  • 🧠 Interactive C++ lessons

  • ✅ Exercises with solution checks

  • 💬 Developer community

Learn to program not alone, but with the support and our Telegram channel 💙

🎯Stop procrastinating

Liked the article?
Time to practice!

In Kodik, you don't just read — you write code immediately. Theory + practice = real skills.

Instant practice
🧠AI explains code
🏆Certificate

No registration • No card