×

C++ STL Tutorial

C++ STL Algorithm

C++ STL Arrays

C++ STL String

C++ STL List

C++ STL Stack

C++ STL Set

C++ STL Queue

C++ STL Vector

C++ STL Map

C++ STL Multimap

C++ STL MISC.

Map in C++ | C++ STL

C++ STL Map and its library functions: Here, we are going to learn about the Map in C++ STL (Standard Template Library).
Submitted by Vivek Kothari, on November 11, 2018

What are Maps in C++ STL?

Maps are associative containers. Maps store elements in form of apair of key value and there mapped value and the key value are stored in sorted manner. Maps are a part of the C++ STL and key values in maps are generally used to identify the elements i.e. there is a value associated with every key. Therefore, there are no two elements in map which have same key and internally, the elements in a map are always sorted by its key. The type of key and elements can be differ in map.

Map Template

Below is the template of a Map in C++ STL:

std::map <key_type, data_type>

Declaration

Below is the syntax of the declaration of a Map in C++ STL:

map <key_type,data_type> myMap;

This line creates a map myMap, where key is of type specified by key_type and element is of type specified by data_type.

C++ STL Map Functions

Now, explore some built in function used in map:

FunctionsDescription
begin() It returns an iterator to the first element
end() It returns an iterator to the element which is just after to the last element
size() It returns the number of elements in the map
empty() It returns whether the map is empty or not
clear() It removes all the elements from the map
pair insert(key_value,map_value) It insert a new element to the map
erase(iterator position) It erase the element at the position pointed by the iterator

Example of a Map in C++ STL

#include <bits/stdc++.h>
using namespace std;

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

  // insert elements in map
  mymap.insert(pair<int, string>(1, "include"));
  mymap.insert(pair<int, string>(2, "help"));
  mymap.insert(pair<int, string>(3, "computer"));
  mymap.insert(pair<int, string>(4, "science"));
  mymap.insert(pair<int, string>(5, "portal"));

  // printing map elements
  map<int, string>::iterator it;
  cout << "Elements in map : " << endl;
  for (it = mymap.begin(); it != mymap.end(); it++) {
    cout << "key = " << it->first << " value = " << it->second << endl;
  }

  // erase element having key = 1
  mymap.erase(1);

  cout << "Elements in map after erasing element having key = 1 : " << endl;
  for (it = mymap.begin(); it != mymap.end(); it++) {
    cout << "key = " << it->first << " value = " << it->second << endl;
  }

  return 0;
}

Output

Elements in map :
key = 1 value = include
key = 2 value = help
key = 3 value = computer
key = 4 value = science
key = 5 value = portal
Elements in map after erasing element having key = 1 :
key = 2 value = help
key = 3 value = computer
key = 4 value = science
key = 5 value = portal

Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.