Map in C++ Standard Template Library (STL) - GeeksforGeeks

17 Jan, 2022
Maps are associative containers that store elements in a mapped fashion. Each element has a key value and a mapped value. No two mapped values can have the same key values.
Some basic functions associated with Map: 
begin() – Returns an iterator to the first element in the map.
end() – Returns an iterator to the theoretical element that follows the last element in the map.
size() – Returns the number of elements in the map.
max_size() – Returns the maximum number of elements that the map can hold.
empty() – Returns whether the map is empty.
pair insert(keyvalue, mapvalue) – Adds a new element to the map.
erase(iterator position) – Removes the element at the position pointed by the iterator.
erase(const g)– Removes the key-value ‘g’ from the map.
clear() – Removes all the elements from the map.
Implementation:
CPP
// CPP Program to demonstrate the implementation in Map#include <iostream>#include <iterator>#include <map>using namespace std; int main(){     // empty map container    map<int, int> gquiz1;     // insert elements in random order    gquiz1.insert(pair<int, int>(1, 40));    gquiz1.insert(pair<int, int>(2, 30));    gquiz1.insert(pair<int, int>(3, 60));    gquiz1.insert(pair<int, int>(4, 20));    gquiz1.insert(pair<int, int>(5, 50));    gquiz1.insert(pair<int, int>(6, 50));    gquiz1.insert(pair<int, int>(7, 10));     // printing map gquiz1    map<int, int>::iterator itr;    cout << "\nThe map gquiz1 is : \n";    cout << "\tKEY\tELEMENT\n";    for (itr = gquiz1.begin(); itr != gquiz1.end(); ++itr) {        cout << '\t' << itr->first << '\t' << itr->second             << '\n';    }    cout << endl;     // assigning the elements from gquiz1 to gquiz2    map<int, int> gquiz2(gquiz1.begin(), gquiz1.end());     // print all elements of the map gquiz2    cout << "\nThe map gquiz2 after"         << " assign from gquiz1 is : \n";    cout << "\tKEY\tELEMENT\n";    for (itr = gquiz2.begin(); itr != gquiz2.end(); ++itr) {        cout << '\t' << itr->first << '\t' << itr->second             << '\n';    }    cout << endl;     // remove all elements up to    // element with key=3 in gquiz2    cout << "\ngquiz2 after removal of"            " elements less than key=3 : \n";    cout << "\tKEY\tELEMENT\n";    gquiz2.erase(gquiz2.begin(), gquiz2.find(3));    for (itr = gquiz2.begin(); itr != gquiz2.end(); ++itr) {        cout << '\t' << itr->first << '\t' << itr->second             << '\n';    }     // remove all elements with key = 4    int num;    num = gquiz2.erase(4);    cout << "\ngquiz2.erase(4) : ";    cout << num << " removed \n";    cout << "\tKEY\tELEMENT\n";    for (itr = gquiz2.begin(); itr != gquiz2.end(); ++itr) {        cout << '\t' << itr->first << '\t' << itr->second             << '\n';    }     cout << endl;     // lower bound and upper bound for map gquiz1 key = 5    cout << "gquiz1.lower_bound(5) : "         << "\tKEY = ";    cout << gquiz1.lower_bound(5)->first << '\t';    cout << "\tELEMENT = " << gquiz1.lower_bound(5)->second         << endl;    cout << "gquiz1.upper_bound(5) : "         << "\tKEY = ";    cout << gquiz1.upper_bound(5)->first << '\t';    cout << "\tELEMENT = " << gquiz1.upper_bound(5)->second         << endl;     return 0;}
The map gquiz1 is : 
    KEY    ELEMENT
    1    40
    2    30
    3    60
    4    20
    5    50
    6    50
    7    10


The map gquiz2 after assign from gquiz1 is : 
    KEY    ELEMENT
    1    40
    2    30
    3    60
    4    20
    5    50
    6    50
    7    10


gquiz2 after removal of elements less than key=3 : 
    KEY    ELEMENT
    3    60
    4    20
    5    50
    6    50
    7    10

gquiz2.erase(4) : 1 removed 
    KEY    ELEMENT
    3    60
    5    50
    6    50
    7    10

gquiz1.lower_bound(5) :     KEY = 5        ELEMENT = 50
gquiz1.upper_bound(5) :     KEY = 6        ELEMENT = 50
 Recent Articles on Map 
YouTubeGeeksforGeeks501K subscribersC++ Programming Language Tutorial | Map in C++ STL | GeeksforGeeksWatch laterShareCopy linkInfoShoppingTap to unmuteIf playback doesn't begin shortly, try restarting your device.You're signed outVideos you watch may be added to the TV's watch history and influence TV recommendations. To avoid this, cancel and sign in to YouTube on your computer.CancelConfirmMore videosMore videosSwitch cameraShareInclude playlistAn error occurred while retrieving sharing information. Please try again later.Watch on0:000:000:00 / 1:50•Live•<div class="player-unavailable"><h1 class="message">An error occurred.</h1><div class="submessage"><a href="https://www.youtube.com/watch?v=kDwXAmLz47w" target="_blank">Try watching this video on www.youtube.com</a>, or enable JavaScript if it is disabled in your browser.</div></div>
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
anshikajain26
cpp-containers-library
cpp-map
STL
C++
STL
CPP
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
Vector in C++ STL
Initialize a vector in C++ (6 different ways)
std::sort() in C++ STL
Socket Programming in C/C++
Bitwise Operators in C/C++
Virtual Function in C++
Templates in C++ with Examples
rand() and srand() in C/C++
unordered_map in C++ STL
Friend class and function in C++