C++ STL Containers

vector, map, set, stack and queue.

✍️ 2 exercises in this lesson

The Standard Template Library provides ready-made containers:

  • std::vector: dynamic array
  • std::map / std::unordered_map: key → value
  • std::set / std::unordered_set: unique values
  • std::stack, std::queue, std::priority_queue
  • std::pair and std::tuple

Key Takeaways: C++ STL Containers

Mastering C++ STL Containers helps you write clean and reliable C++ code. Follow standard conventions, test edge cases, and keep logic simple. Try running the code example below and complete the exercises to build confidence.

C++ STL Containers Code Example

C++
#include <iostream>
#include <map>
#include <set>
#include <stack>
#include <string>

int main() {
    std::map<std::string, int> stock = {{"Laptop", 12}, {"Mouse", 150}};
    stock["Monitor"] = 25;
    for (const auto& [item, qty] : stock) std::cout << item << ": " << qty << "\n";

    std::set<int> unique = {3, 1, 3, 2, 1};
    std::cout << "Unique count: " << unique.size() << "\n";

    std::stack<std::string> history;
    history.push("home"); history.push("about"); history.push("contact");
    history.pop();
    std::cout << "Back to: " << history.top() << "\n";
    return 0;
}
Output
Laptop: 12
Monitor: 25
Mouse: 150
Unique count: 3
Back to: about
Run this example on your own computer. The "Get Started" lesson shows how to set it up.

C++ STL Containers Practice Exercises

Check what you learned in “C++ STL Containers”.

/ 2 solved
Fill in the blank

Create a map from string keys to int values.

std::<std::string, int> stock;
✓ Correct! Not quite. Try again.
Multiple choice

Which container is LIFO (last in, first out)?

✓ Correct! Not quite. Try again.
to save your exercise progress.
Finished this lesson?
Mark it complete to track your progress.

Stuck on something?

Ask the community in the CodesInTech forum.

Need a developer?

Learning on your own is great — and when you need professional help, our team builds custom software, websites and mobile apps for clients worldwide.

Explore Our Services