OnlineCBT Logo OnlineCBT
C++ · LESSON 16

STL Maps, Sets, and Unordered Containers

By Ratan Updated: 15 Oct 2026

Store key-value pairs and unique elements using std::map, std::set, and hash-based std::unordered_map.

Level: Beginner to Advanced
Duration: ~60 Mins Deep-Dive

Look up values by a key

std::map stores unique keys in sorted order. std::set stores unique values in sorted order. Their unordered counterparts organize values with a hash function and do not promise a useful iteration order. Choose based on ordering needs and lookup behavior.

Frequency counter example

#include 
#include 
#include 
#include 
#include 
int main() {
    std::map counts;
    for (const auto& word : {"C", "C++", "C", "HTML"}) ++counts[word];
    for (const auto& entry : counts) {
        std::cout << entry.first << ':' << entry.second << '\n';
    }
    std::set rolls{103, 101, 103};
    std::cout << "unique rolls=" << rolls.size() << '\n';
    std::unordered_map names{{101, "Asha"}};
    auto found = names.find(101);
    if (found != names.end()) std::cout << found->second << '\n';
}

The ordered map prints C:2, C++:1 and HTML:1 in key order. The set has two unique rolls. Lookup of 101 prints Asha. Do not write a test that assumes a particular traversal order for an unordered_map.

operator[] can modify the map

counts[key] inserts a missing key with a value-initialized mapped value. That is convenient for counting, where int begins at zero. For a read-only existence check, use find in C++17. at accesses an existing mapped value and throws if the key is absent. Do not accidentally create records simply by checking whether they exist.

Cost and invalidation

Ordered maps and sets offer logarithmic search and insertion. Unordered containers typically provide average constant-time lookup, but worst cases can be linear. Hashing quality, key size and workload matter. Rehashing invalidates iterators in unordered containers; do not retain an iterator across an operation that may rehash without checking the documented guarantees.

Key rules

Map keys cannot be modified in place through an iterator because changing a key could violate the container's organization. An ordered comparator must provide a strict weak ordering. Hash and equality operations for an unordered container must agree that equivalent keys have the same hash.

Practice

  1. Count subject names in a vector and report the most frequent.
  2. Insert the same roll twice in a set; its size should increase only once.
  3. Compare map.find("missing") with map["missing"] by observing the size.
  4. Choose map when reports require sorted keys; choose unordered_map only when ordering is unnecessary and its tradeoffs suit the workload.

📝 Live Lesson Practice

HTML/CSS JavaScript Python C++ C PHP
⌨️ Practice Inputs (लाइव इनपुट भरें) (खाली होने पर RED, भरने पर GREEN underline)
💻 Code Editor (Monaco VS Code Engine)
👀 Live Preview
Address Contact

+91 7877547686

E-mail

onlinecbtportal@gmail.com

Helpline Number

+91 7877547686


Click To Download
Get it on Google Play

ऐप डाउनलोड करने
के लिए Google Play पर
उपलब्ध है

WhatsApp Chat