Home
Aptitude
Categories


Home » C++ STL

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

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:

    std::map <key_type, data_type>

Syntax of declaration:

    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.

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:

#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

Ad: Are you a blogger? Join our Blogging forum.





Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing » Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.