Home » C++ Tutorial » C++ Map with Examples

C++ Map with Examples

It is an associative container to store the data in the form of key and value pairs. We can not store the same value in two maps. That means we need to provide the unique key-value pairs to the map. To import a map in c++ program, add the map header file.

Uses of the map in c++

There are two main uses of maps for programmers. First is the map does not allow data duplication so programmers can identify the specific data accurately. This the best feature provided by a map. And second is using the keys to map the searching is way faster than other techniques. This feature helps while developing large scale applications. While developing a large application in the c++, the map is one thing that developers like to use.

How to use a C++ map?

C++ compiler provides the map header file. We need to include it in the program where we want to use the map. Also, C++ has provided some built-in functions for the map to simplify the work of programmers. Using those built-in function, we can easily do operations on the map.

Built-in functions in the map header file

  1. begin() – This function returns the iterator pointing to the first element in the map.
  2. size() – This function returns an integer value of the number of elements present in the map.
  3. empty() – This function return true if map is empty or return false.
  4. insert(pair(key, value) – This function is used to add the element in the map. To add the element in the map wee need to pass two arguments to the function first is the key, it should be unique, and the second the value that will store with the key. 
  5. at(key): This function will access the value of the key pair provided in the parameter.
  6. find(value): C++ map find() function will find the value passed to it.
  7. erase(position): This function will remove the element at the given position passed as a parameter.
  8. erase(key): This function will remove the key passed in the parameter.
  9. count(key): This function will return the number of values that are equivalent to the passed key as a parameter. 
  10. clear(): To remove all the elements from the map this function is used.

Syntax of a map

map<datatype1, datatype2>map_name ;

C++ map examples

Write a program to create a map in c++, insert 5 pairs in it and display the result.

#include <map> // Header file to include map in the code
#include<iostream>
#include <string>
using namespace std;

int main() 
{
	map<int, string> mp; //creating a map
	map<int, string>::iterator it; //object for forloop
	  
	mp.insert(pair<int, string>(1, "Python"));  //inserting values
	mp.insert(pair<int, string>(2, "C")); 
	mp.insert(pair<int, string>(3, "Java"));
	mp.insert(pair<int, string>(4, "PHP"));
	mp.insert(pair<int, string>(5, "C++"));
	
	mp[6] = "Pearl"; //other way to add element in map
	mp[7] = "JavaScript";
	mp[8] = "R Programming";
	mp[9] = "Scala";
	mp[10] = "Rust";
	  
	std::cout<<endl<<"Map : "<<endl;
	for (it = mp.begin(); it != mp.end(); it++) {
	  std::cout<< it->first << " " << it->second << endl; //Printing the values after inserting
	}
	
	mp.erase(10); // removing 10th element
	mp.erase(9);  // removing 9th element
	std::cout<<endl<<"After removing 2 elements from the map"<<endl;
	
	for (it = mp.begin(); it != mp.end(); it++) {
	  std::cout<< it->first << " " << it->second << endl;
	}
	 	
	mp.erase(mp.find(6), mp.end()); //removing multiple elements from the map
	  
	std::cout<<endl<<"After removing multiple elements from the map"<<endl;
	
	for (it = mp.begin(); it != mp.end(); it++) {
	  std::cout<< it->first << " " << it->second << endl;
	}
	
	std::cout<<endl<<"Size of the map"<<mp.size(); //this will return the number of elements present in the map
	
	it = mp.find('c'); 
      
    if(it == mp.end()) 
        cout <<endl<< "Key-value pair not present in a map" ; 
    else
        cout <<endl<< "\nKey-value pair present : " 
          << it->first << "->" << it->second ; 
		 
    it = mp.find('SQL'); 
      
    if(it == mp.end()) 
        cout <<endl<< "\nKey-value pair not present in map" ; 
    else
        cout <<endl<< "\nKey-value pair present : " 
          << it->first << "->" << it->second ; 

	std::cout<< endl;
}

Output

Map :
1 Python
2 C
3 Java
4 PHP
5 C++
6 Pearl
7 JavaScript
8 R Programming
9 Scala
10 Rust

After removing 2 elements from the map
1 Python
2 C
3 Java
4 PHP
5 C++
6 Pearl
7 JavaScript
8 R Programming

After removing multiple elements from the map
1 Python
2 C
3 Java
4 PHP
5 C++

Size of the map5

Key-value pair present : 2->C

Key-value pair not present in a map

What is the hash map in c++?

Hash map is nothing but the unordered map. Hash map stores the data in the unordered form. The difference between a map and a hash map is the map stores data in ordered form whereas the hash map stores the data in an unordered form. The Hash map has the same functions as a map in c++. You can simply use add, delete, find, count, size, etc functions on the hash map

Example of a hash map in c++

#include<iostream>
#include<unordered_map> 
using namespace std; 
  
main() 
{  
    unordered_map<int, string> ump;  //Creating the hash map
    unordered_map<int, string>::iterator it;
    
    ump.insert(pair<int, string>(1, "C"));  //Inserting values in the hash map
    ump.insert(pair<int, string>(2, "C++"));
    ump.insert(pair<int, string>(3, "Core Java"));
    ump.insert(pair<int, string>(4, "Advance Java"));
    ump.insert(pair<int, string>(5, "Python"));
    
    ump[6] = "Scala"; //Another way to insert values in hash map
    ump[7] = "rust";
    ump[8] = "R Programming";
    ump[9] = "PHP";
    ump[10] = "SQL";

	std::cout<<endl<<"Hash Map (In unsorted format) : "<<endl;
	for (it = ump.begin(); it != ump.end(); it++) {
	  std::cout<< it->first << " " << it->second << endl; //printing hash map
	}    
    
    it = ump.find(2); //Finding an element
      
    if(it == ump.end()) 
        cout <<endl<< "Key-value pair not present in hash map" ; 
    else
        cout <<endl<< "Key-value pair present : " 
          << it->first << "->" << it->second ; 

}

it = ump.find('JavaScript'); 
      
    if(it == ump.end()) 
        cout <<endl<< "Key-value pair not present in hash map" ; 
    else
        cout <<endl<< "Key-value pair present : " 
          << it->first << "->" << it->second ;
}

Output

Hash Map (In unsorted format) :                                                                                                                 
10 SQL                                                                                                                                          
9 PHP                                                                                                                                           
8 R Programming                                                                                                                                 
7 rust                                                                                                                                          
2 C++                                                                                                                                           
1 C                                                                                                                                             
3 Core Java                                                                                                                                     
4 Advance Java                                                                                                                                  
5 Python                                                                                                                                        
6 Scala

Key-value pair present : 2->C++

Key-value pair not present in the hash map

How to iterate through a map c++?

map<int, string>::iterator it;
for (it = mp.begin(); it != mp.end(); it++) {
	  std::cout<< it->first << " " << it->second << endl;
	}
Did you know?
1. For Loop In C++
2. Vectors In C++
3. Difference Between C and C++
4. C++ Swap
5. C++ List
6. C++ Hello World
7. C++ Map
8. Converting String To Int C++
9. Stack C++
10. Queue C++

Pin It on Pinterest