Maps and Sets in Cpp

9/18/2025

diagram Maps Sets in Cpp

Go Back

Maps and Sets in C++

Introduction

Maps and Sets are part of the associative containers in the Standard Template Library (STL) of C++. They are used to store data in a sorted and efficient manner with unique keys. These containers are ideal for fast searching, insertion, and deletion operations.

This article explores the concepts, types, and usage of maps and sets with examples, making it easy for beginners to understand.


 diagram Maps  Sets in Cpp

What are Associative Containers?

Associative containers store elements in a key-based structure rather than a linear sequence. This allows faster lookup (logarithmic time complexity).

The two main types are:

  • Set – Stores unique elements only

  • Map – Stores key-value pairs (keys are unique)


Sets in C++

A set is a collection of unique elements stored in sorted order by default.

Syntax

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

int main() {
    set<int> numbers;

    numbers.insert(5);
    numbers.insert(2);
    numbers.insert(8);
    numbers.insert(2); // Duplicate, will be ignored

    for (int n : numbers) {
        cout << n << " ";
    }
    return 0;
}

Output: 2 5 8

Common Member Functions of Set

FunctionDescription
insert()Adds an element (ignores duplicates)
erase()Removes an element
find()Finds an element
count()Returns 1 if element exists
size()Returns number of elements
empty()Checks if set is empty

Multiset in C++

Multiset is similar to a set but allows duplicate elements.

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

int main() {
    multiset<int> ms = {1, 2, 2, 3};
    ms.insert(2);

    for (int n : ms) {
        cout << n << " ";
    }
    return 0;
}

Output: 1 2 2 2 3


Maps in C++

A map stores key-value pairs in sorted order by keys. Each key is unique.

Syntax

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

int main() {
    map<string, int> marks;

    marks["Alice"] = 90;
    marks["Bob"] = 85;
    marks["Charlie"] = 92;

    for (auto &pair : marks) {
        cout << pair.first << ": " << pair.second << "\n";
    }
    return 0;
}

Output:

Alice: 90
Bob: 85
Charlie: 92

Common Member Functions of Map

FunctionDescription
insert()Inserts key-value pair
erase()Removes an element by key
find()Finds an element by key
count()Checks if key exists (1 if found)
size()Returns number of elements
empty()Checks if map is empty

Multimap in C++

Multimap is similar to a map but allows multiple pairs with the same key.

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

int main() {
    multimap<string, int> scores;
    scores.insert({"Alice", 90});
    scores.insert({"Alice", 85});

    for (auto &p : scores) {
        cout << p.first << ": " << p.second << "\n";
    }
    return 0;
}

Difference Between Map vs Set

FeatureSetMap
Data StructureStores only keysStores key-value pairs
DuplicatesNot allowed (except multiset)Not allowed (except multimap)
AccessUses iteratorsAccess via keys (map[key])
OrderSorted orderSorted order by keys

When to Use Sets and Maps

  • Use set when you only need to store unique items.

  • Use multiset if duplicates are required.

  • Use map when you need key-value mapping.

  • Use multimap when multiple values can be associated with a single key.


Conclusion

Maps and Sets are crucial STL containers in C++ for managing sorted collections and key-value data efficiently. They offer fast searching, insertion, and deletion, making them ideal for real-world applications like dictionaries, contact lists, and databases.