• Skip to main content
  • Skip to secondary menu
  • Skip to primary sidebar
  • Skip to footer

Mr.CodeHunter

Programming and Code Solutions

  • Home
  • C++
  • About Us
  • Contact Us
  • Privacy Policy

C++ Map [Learn by Example]

August 21, 2021 by Admin Leave a Comment

C++ map is part of Standard Template Library (STL). It is type of Associative container. Map in c++ is used to store unique key and it’s value in data structure. But if you want to store non-unique key value then you can use Multi Map in c++. Let us first understand in detail what is map in c++ by examples.

C++ MAP understanding
C++ MAP understanding

In given image you can see that c++ map can store key and value pair. Just assume you have dynamic array which can store key and value where key and value can be with different data types. Here in Student Map example we are storing student roll Number as KEY and Name of student as Value. So for whole class we can store student number and name easily. It is sure that you can not insert similar Number again in map data structure.

Syntax of c++ map

// Require header file inclusion
#include <map>

// Syntax of Map
map<key, value> ObjectOfMap;

// Examples
map<int, string> obj1;  // Integer Key and String Value
map<int, int> obj2;   // Key and value both Integer

For using map in c++ code you will require to add #include <map> inside your header. Map is internal implemented with Template in c++, so you will see “< >” open and closed angle in map syntax. So you can pass any dynamically data type inside it easily and it will work for all data typed generically.

Different functions supported by c++ map

Map c++ support different types of functions which can be performed data stored in map.

c++ map functions
c++ map functions

These are just examples of few map functions but there are many more functions available for c++ map.

map::insert :- It is use to insert key value pair data inside map.
map::clear :- We can clear map data using this function.
map::begin :- It return iterator of map beginning. Use to iterate map.
map::end :- It return iterator of map ending. Use to iterate map.
map::find :- Use to find key value pair from map.
map::erase :- Use to erase data from map.
map::empty :- Check map is empty or not.
map::size :- Get elements available in map.
map::at :- Get value at particular key.

How to insert in c++ map ?

Using map::insert function and using std::make_pair or pair we can insert data in map.

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

int main()
{
	// Create Map object
	map<int, string> ObjMap;

	//Insert key-value data inside map
	ObjMap.insert(std::make_pair(1, "John"));

	cout << "Value: " << ObjMap[1].c_str();
}

How to insert structure data in c++ Map ?

We can insert structure of data inside value of map. So key will be unique and it’s value contain structure of data, which can be bunch of multiple data.

c++ map store structure of data
c++ map store structure of data

Here we are storing full student information inside map using storing StudentInfo structure object.

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

// Student information structure
struct StudentInfo {
	string Name;
	int    Age;
	string Address;
	string FatherName;
	string ContactNumber;
};

int main()
{
	// Object of structure
	StudentInfo ObjStudent;

	// Adding data inside structure
	ObjStudent.Name = "John";
	ObjStudent.Age = 25;
	ObjStudent.FatherName = "Miller";
	ObjStudent.Address= "N-404, Sigma city";
	ObjStudent.ContactNumber = "5252552";

	// Creating Map
	map<int, StudentInfo> StudentMap;

	// Inserting data into map
	StudentMap.insert(std::make_pair(1, ObjStudent));
	return 0;
}

Map iterator in c++

If we want to make loop of map from beginning to end then we have to use iterator to iterate map from begin() to end().

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

int main()
{
	// Create Map object
	map<int, string> ObjMap;

	//Insert key-value data inside map
	ObjMap.insert(std::make_pair(1, "John"));
	ObjMap.insert(std::make_pair(2, "Jimi"));
	
	// Create Map iterator
	map<int, string>::iterator itr;

	// Loop iterator for map
	for (itr = ObjMap.begin(); itr != ObjMap.end(); ++itr)
	{
		printf("%d : '%s'\n", (*itr).first, (*itr).second.c_str());
	}

	// Output
	// 1 : John
	// 2 : Jimi

	return 0;
}

Using c++11/c++14 you can iterate map without using iterator. You can use auto keyword which will automatically understand type of returning value. It will understand that it is iterator and then it will work accordingly.

// C++11 / C++14	
for (const auto& kv : ObjMap) 
{
  printf("%d : '%s'\n", kv.first, kv.second.c_str());
}

Find in map c++ / c++ map check if key exist

We can find value in map using map::find(key) function. We need to pass key inside find function and then check that it is not map::end(). If it reach to end and still not found value then it is clear then key is not found.

	// Create Map object
	map<int, string> ObjMap;
	//Insert key-value data inside map
	ObjMap.insert(std::make_pair(1, "John"));
	ObjMap.insert(std::make_pair(2, "Jimi"));
	
	// Create Map iterator
	map<int, string>::iterator itr;

	// Assign find iterator value
	itr = ObjMap.find(2);

	// Check if iterator is Ended then not found
	if (itr != ObjMap.end())
	{
		cout << "Found";
	}
	else {
		cout << "Not Found";
	}

c++ map get value by key

We can get value by map:: find function. It return iterator and from iterator first and second value we can find key and value information easily.

	// Create Map object
	map<int, string> ObjMap;
	//Insert key-value data inside map
	ObjMap.insert(std::make_pair(1, "John"));
	ObjMap.insert(std::make_pair(2, "Jimi"));
	
	// Create Map iterator
	map<int, string>::iterator itr;

	// assign find iterator value
	itr = ObjMap.find(2);

	// If found
	if (itr != ObjMap.end())
	{
		printf("%d : '%s'\n", itr->first, itr->second.c_str());
		//Output:
		// 2 : Jimi
	}

Conclusion of c++ map

  1. c++ map is part of STL (Standard Template Library).
  2. c++ map is used to store unique key-value pair.
  3. Require to include #include <map> header file
  4. It contain many functions like insert, find, size ..etc

For More C++ :

TOP BEST C++ IDE FOR DEVELOPERS

Filed Under: All C++, C++, C++11

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Primary Sidebar

Search here

Social Media

  • Facebook
  • YouTube

SEE MORE

Fibonacci sequence c++

Fibonacci Sequence c++ is a number sequence which created by sum of previous two numbers. First two number of series are 0 and 1. And then using these two number Fibonacci series is create like 0, 1, (0+1)=1, (1+1)=2, (2+1)=3, (2+3)=5 …etc Displaying Fibonacci Series in C++ ( without recursion) Output: From given output we […]

map c++

C++ Map [Learn by Example]

C++ map is part of Standard Template Library (STL). It is type of Associative container. Map in c++ is used to store unique key and it’s value in data structure. But if you want to store non-unique key value then you can use Multi Map in c++. Let us first understand in detail what is […]

how to copy paste in turbo c++ ?

There are many different C++ IDE are available but still many students are using Turbo c++ for learning c/c++ programming languages. During using Turbo c++ if you are beginner you will be confuse for how to copy and paste in turbo c++ or if you have already copy some content and you want to paste […]

C++

return 0 c++

There are two different scenario return statement is used inside c++ programming. We can use return 0 c++ inside main() function or other user defined functions also. But both have different meanings. return 0 c++ used inside Main function return 0 c++ used inside other user defined function What is meaning of return 0 and […]

C++

c++ expected a declaration [ SOLVED]

When any function or statement is not in scope or we have used wrong syntax then possibly you will get error for c++ expected a declaration in your code. Main reasons for errors are: Incorrect use/declaration inside namespace Statements are added out of scope Required statement need to add inside main/function Solution-1 | Expected a […]

C++

c++ cannot open source file “errno.h” [SOLVED]

Normally you will face c++ cannot open source file “errno.h” in MS Visual Studio c++ projects. These are some solutions to remove opening errors for “errno.h” file. I got the errors to go away by installing the Windows Universal CRT SDK component, which adds support for legacy Windows SDKs. You can install this using the […]

Footer

DISCLAIMER

The information contained on https://www.mrcodehunter.com is for general information purposes only. We assumes no responsibility for errors or omissions in the contents on the Service.

SITEMAP XML

Sitemap

Recent

  • Fibonacci sequence c++
  • C++ Map [Learn by Example]
  • how to copy paste in turbo c++ ?
  • return 0 c++
  • c++ expected a declaration [ SOLVED]

Search

Copyright © 2025 ยท Mr Code Hunter