Maps

Maps in C++ are part of the Standard Template Library (STL). A map is a container that stores data in key-value pairs, where each key is unique.

What is a Map?

A map is a data structure that stores elements in pairs:

  • Key → Unique identifier
  • Value → Data associated with the key

Each key maps to exactly one value.

Why Use Maps?

Maps are useful because they:

  • Store data in an organized way
  • Allow fast searching using keys
  • Ensure unique keys
  • Automatically sort data by keys

Map Header File

To use maps in C++, include the <map> library.

#include <map>

Declaring a Map

map<string, int> studentMarks;

Adding Elements

You can insert values using the key.

studentMarks["Ali"] = 85;
studentMarks["Ahmed"] = 90;

Accessing Elements

cout << studentMarks["Ali"];

Example Program of Map

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

int main() {

map<string, int> marks;

marks["Ali"] = 80;
marks["Sara"] = 90;
marks["Ahmed"] = 85;

for (auto it : marks) {

cout << it.first << " : " << it.second << endl;
}

return 0;
}

Output

Ahmed : 85
Ali : 80
Sara : 90

Common Map Functions

FunctionPurpose
insert()Add element
erase()Remove element
find()Search key
size()Get number of elements
empty()Check if map is empty

Example of insert and find

marks.insert({"Zain", 75});

if (marks.find("Ali") != marks.end()) {

cout << "Found";
}

How Map Works

  1. Stores data in key-value pairs
  2. Keys are always unique
  3. Data is automatically sorted by key
  4. Uses balanced tree structure internally

Types of Maps

1. Ordered Map

  • Stores data in sorted order
  • Uses map

2. Unordered Map

  • Faster access
  • No sorting
  • Uses unordered_map

Difference Between Map and Vector

MapVector
Key-value storageIndex-based storage
Unique keysNo key concept
Sorted by keyNot sorted automatically

Important Points About Maps

  • Keys must be unique
  • Values can repeat
  • Automatically sorted by key
  • Fast lookup using keys

Advantages of Maps

  • Efficient searching
  • Organized data storage
  • Easy key-based access
  • Useful for large datasets

Common Mistakes with Maps

  • Using duplicate keys incorrectly
  • Assuming insertion order is preserved
  • Accessing non-existing keys without checking
  • Confusing map with array indexing

Real-Life Example

Think of a dictionary:

  • Word = Key
  • Meaning = Value

You search by word to get meaning. This is how a map works.

Applications of Maps

Maps are used in:

  • Student record systems
  • Phone directories
  • Database indexing
  • Caching systems
  • Configuration settings

Why Maps are Important

Maps are important because they:

  • Provide fast data retrieval
  • Organize data efficiently
  • Support real-world applications
  • Improve program performance

Conclusion

Maps in C++ are powerful STL containers that store data in key-value pairs. They provide fast searching, organized storage, and are widely used in real-world applications where efficient data access is required.

Home » Professional C++ > STL (Standard Template Library) > Maps