Industrial manufacturing
Industrial Internet of Things | Industrial materials | Equipment Maintenance and Repair | Industrial programming |
home  MfgRobots >> Industrial manufacturing >  >> Industrial programming >> C Language

Mastering std::map in C++: Comprehensive Guide with Code Examples

What is std::map?

In the C++ Standard Template Library (STL), std::map is an associative container that stores elements as key‑value pairs. Each key is unique and the container automatically keeps the elements sorted according to the key’s comparison function (by default, std::less). The values can be of any type and are accessed through their corresponding keys.

Why use std::map?

Syntax

Declare a map with:

std::map<KeyType, ValueType> mapName;

Example:

std::map<std::string, int> ageMap;

Member Types

Built‑in Functions

Iterating Over Map Elements

Iterators give you full control over traversal order, which is always sorted by key.

#include <iostream>
#include <string>
#include <map>

using namespace std;

int main() {
    map<int, string> students;
    students.insert({200, "Alice"});
    students.insert({201, "John"});

    cout << "Map size is: " << students.size() << endl;
    cout << endl << "Default map order:" << endl;

    for (auto it = students.begin(); it != students.end(); ++it) {
        cout << it->first << ": " << it->second << endl;
    }
    return 0;
}

Output:

Mastering std::map in C++: Comprehensive Guide with Code Examples

Inserting Data into std::map

Use insert() for new entries; it silently rejects duplicates. To overwrite an existing key, use insert_or_assign().

#include <map>
#include <iostream>

using namespace std;

int main() {
    map<int, int> m{{1,3},{2,4},{3,5}};
    m.insert({5,6});        // adds new pair
    m.insert({1,8});        // ignored, key 1 already exists
    m.insert_or_assign(1,6); // updates key 1 to value 6

    cout << "Key\tElement\n";
    for (auto& p : m) {
        cout << p.first << '\t' << p.second << '\n';
    }
    return 0;
}

Output:

Mastering std::map in C++: Comprehensive Guide with Code Examples

Searching in a Map

Finding a key is straightforward with find(). If the key exists, the returned iterator points to the element; otherwise end() is returned.

#include <iostream>
#include <map>

using namespace std;

int main() {
    map<int, string> students;
    students.insert({200, "Alice"});
    students.insert({201, "John"});

    if (auto it = students.find(201); it != students.end()) {
        cout << "Key 201 has value: " << it->second << endl;
    }
    return 0;
}

Output:

Mastering std::map in C++: Comprehensive Guide with Code Examples

Deleting Data from a Map

Elements can be removed by key or by iterator. The erase() method invalidates only iterators pointing to the erased element.

#include <iostream>
#include <map>

using namespace std;

int main() {
    map<string, int> myMap;
    myMap["cow"] = 1;
    myMap["cat"] = 2;
    myMap["lion"] = 3;

    if (auto it = myMap.find("cat"); it != myMap.end()) {
        myMap.erase(it);
    }

    for (auto& p : myMap) {
        cout << p.first << ": " << p.second << endl;
    }
    return 0;
}

Output:

Mastering std::map in C++: Comprehensive Guide with Code Examples

Summary

C Language

  1. C++ For Loops Explained: Syntax, Workflow, and Practical Examples
  2. Mastering C++ Switch‑Case: Syntax, Usage, and Practical Examples
  3. Master C++ Dynamic Array Allocation: A Practical Guide with Code Examples
  4. Mastering std::stack in C++: A Comprehensive Guide with Practical Examples
  5. C++ Structs Explained with a Practical Example
  6. Mastering C++ Vectors: Dynamic Arrays, Iterators, and Practical Examples
  7. C++ Classes & Objects: A Practical Guide with Code Examples
  8. C++ Polymorphism Explained: Practical Examples & Key Concepts
  9. Mastering std::list in C++: Syntax, Functions & Practical Examples
  10. Dynamic Memory Allocation in C: Understanding malloc() with Practical Examples