๐Ÿ‡ฎ๐Ÿ‡ณ India's Free Coding Tutorial โ€” Learn C, PHP, MySQL, Python, Java About ยท Contact
Advertisement

Maps & Sets

C++ Language Templates & STL ๐Ÿ“… Mar 2026 โฑ 5 min read ๐Ÿ†“ Free

map โ€” Key-Value Store

cpp
#include <iostream>
#include <map>
#include <set>
using namespace std;

int main() {
    // map - sorted by key
    map<string, int> marks;
    marks["Gagan"] = 95;
    marks["Priya"] = 88;
    marks["Raj"]   = 72;
    
    cout << marks["Gagan"] << endl;  // 95
    
    for (auto& [name, mark] : marks)  // structured binding (C++17)
        cout << name << ": " << mark << endl;
    
    // Check if key exists
    if (marks.count("Priya")) cout << "Priya found
";
    
    // set - unique sorted elements
    set<int> s = {5, 3, 8, 3, 1, 5};  // duplicates removed
    for (int x : s) cout << x << " ";  // 1 3 5 8
    
    return 0;
}
Advertisement
โ† Back to C++ Language Index