WikiGalaxy

Personalize

C++ Maps: Introduction

What are C++ Maps?

In C++, a map is a container that stores elements in key-value pairs. It is part of the Standard Template Library (STL) and is implemented as a balanced binary tree. The keys in a map are unique, and the values can be accessed using these keys.

Key Features:

Maps automatically sort their elements by key, allow fast retrieval of values using keys, and provide iterators to traverse through the elements.


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

int main() {
    map<string, int> age;
    age["Alice"] = 30;
    age["Bob"] = 25;
    cout << "Alice's age: " << age["Alice"] << endl;
    return 0;
}
    

Console Output:

Alice's age: 30

Basic Operations on Maps

Inserting Elements:

Elements can be inserted into a map using the insert() function or by using the subscript operator [].

Accessing Elements:

You can access elements by their keys using the subscript operator []. If the key does not exist, a new entry is created.

Deleting Elements:

Elements can be removed using the erase() function, which takes a key or an iterator as an argument.


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

int main() {
    map<string, int> age;
    age.insert(make_pair("Charlie", 35));
    age["David"] = 40;
    age.erase("Charlie");
    cout << "David's age: " << age["David"] << endl;
    return 0;
}
    

Console Output:

David's age: 40

Iterating Over Maps

Using Iterators:

Maps provide iterators to traverse through all elements. You can use a for loop with iterators to access each key-value pair.


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

int main() {
    map<string, int> age = {{"Eve", 28}, {"Frank", 33}};
    for (auto it = age.begin(); it != age.end(); ++it) {
        cout << it->first << ": " << it->second << endl;
    }
    return 0;
}
    

Console Output:

Eve: 28

Frank: 33

Checking Existence of a Key

Using find() Method:

The find() method returns an iterator to the element if the key exists, or the end iterator if it does not.


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

int main() {
    map<string, int> age = {{"Grace", 29}, {"Hank", 31}};
    if (age.find("Grace") != age.end()) {
        cout << "Grace is found." << endl;
    } else {
        cout << "Grace is not found." << endl;
    }
    return 0;
}
    

Console Output:

Grace is found.

Counting Elements

Using size() Method:

The size() method returns the number of elements in the map.


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

int main() {
    map<string, int> age = {{"Ivy", 24}, {"Jack", 27}};
    cout << "Number of people: " << age.size() << endl;
    return 0;
}
    

Console Output:

Number of people: 2

Clearing a Map

Using clear() Method:

The clear() method removes all elements from the map, leaving it empty.


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

int main() {
    map<string, int> age = {{"Karen", 32}, {"Leo", 29}};
    age.clear();
    cout << "Map size after clear: " << age.size() << endl;
    return 0;
}
    

Console Output:

Map size after clear: 0

Copying Maps

Using Copy Constructor:

You can create a copy of a map using the copy constructor.


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

int main() {
    map<string, int> age = {{"Mona", 26}, {"Nate", 34}};
    map<string, int> ageCopy(age);
    cout << "Copy size: " << ageCopy.size() << endl;
    return 0;
}
    

Console Output:

Copy size: 2

Swapping Maps

Using swap() Method:

The swap() method exchanges the contents of two maps.


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

int main() {
    map<string, int> age1 = {{"Olivia", 22}};
    map<string, int> age2 = {{"Paul", 45}};
    age1.swap(age2);
    cout << "Age1 size after swap: " << age1.size() << endl;
    return 0;
}
    

Console Output:

Age1 size after swap: 1

Using Custom Comparator

Custom Sorting Criterion:

You can define a custom sorting criterion for a map by using a comparator function.


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

struct CustomCompare {
    bool operator()(const string &a, const string &b) const {
        return a.length() < b.length();
    }
};

int main() {
    map<string, int, CustomCompare> age = {{"Quinn", 23}, {"Riley", 29}};
    for (auto it = age.begin(); it != age.end(); ++it) {
        cout << it->first << ": " << it->second << endl;
    }
    return 0;
}
    

Console Output:

Quinn: 23

Riley: 29

logo of wikigalaxy

Newsletter

Subscribe to our newsletter for weekly updates and promotions.

Privacy Policy

 • 

Terms of Service

Copyright © WikiGalaxy 2025