ProDeveloperTutorial.com

Tutorials and Programming Solutions
Menu
  • Shell Scripting
  • System Design
  • Linux System Programming
  • 4g LTE
  • Coding questions
  • C
  • C++
  • DSA
  • GIT
  • 450 DSA Cracker
  • 5G NR
  • O-RAN

CPP STL Chapter 8: std::map and it’s operations.

prodevelopertutorial March 28, 2020
In this chapter we shall learn about:
1. map introduction.
2. map declaration.
3. Multidimensional Map
4. Passing std::map to function
5. map member function to iterate over elements.
6. map member function to check the capacity.
7. map member function to access the elements.
8. map member function to modify the elements.

1. map introduction.

1. Maps are associative containers.
2. They are used to hold key value pairs.
3. key values are generally used to sort and uniquely identify the elements.
4. The elements in a map are always sorted by its key.
5. map are generally slower than unordered_map containers to access individual elements by their key.
You can use “pair”.

typedef pair<const Key, T> value_type;
You need to use below header file to use map:

#include <map>

2. map declaration.

map can be declared as below:

  std::map<char,int> mymap;

  mymap['b'] = 100;
  mymap['a'] = 200;
  mymap['c'] = 300;

3. Multidimensional Map

Syntax to create 2D map:

map< key_1_type, map< key_2_type, value_type> > object;

4. Passing std::map to function

———————————————————————————-
You can pass a map in 2 ways:
1. Pass by value
2. Pass by reference
1. Pass by value

void func(map<char,int> mymap) 
{ 
  mymap['d'] = 400;
} 
   
   
int main ()
{
  std::map<char,int> mymap;

  mymap['b'] = 100;
  mymap['a'] = 200;
  mymap['c'] = 300;

  func(mymap);
  std::cout << "mymap contains using .begin:"<<endl;
  for (std::map<char,int>::iterator it=mymap.begin(); it!=mymap.end(); ++it)
    std::cout << it->first << " => " << it->second << '\n';


  return 0;
}
2. Pass by reference

void func(map<char,int> &mymap) 
{ 
  mymap['d'] = 400;
} 
   
   
int main ()
{
  std::map<char,int> mymap;

  mymap['b'] = 100;
  mymap['a'] = 200;
  mymap['c'] = 300;

  func(mymap);
  std::cout << "mymap contains using .begin:"<<endl;
  for (std::map<char,int>::iterator it=mymap.begin(); it!=mymap.end(); ++it)
    std::cout << it->first << " => " << it->second << '\n';


  return 0;
}

5. map member function to iterate over elements.

begin : It will return iterator to beginning
end : It will return iterator to end
rbegin : It will return reverse iterator to reverse beginning
rend : It will return reverse iterator to reverse end
cbegin  : It will return const_iterator to beginning
cend   : It will return const_iterator to end

#include <iostream>
#include <map>
//for more tutorials on C, C++, STL, DS visit www.ProDeveloperTutorial.com
using namespace std;

int main ()
{
  std::map<char,int> mymap;

  mymap['b'] = 100;
  mymap['a'] = 200;
  mymap['c'] = 300;

  std::cout << "mymap contains using .begin:"<<endl;
  for (std::map<char,int>::iterator it=mymap.begin(); it!=mymap.end(); ++it)
    std::cout << it->first << " => " << it->second << '\n';

  std::cout << "mymap contains using .rbegin:"<<endl;
  std::map<char,int>::reverse_iterator rit;
  for (rit=mymap.rbegin(); rit!=mymap.rend(); ++rit)
    std::cout << rit->first << " => " << rit->second << '\n';

  std::cout << "mymap contains using .cbegin:"<<endl;
   for (auto it = mymap.cbegin(); it != mymap.cend(); ++it)
    std::cout << it->first << " => " << it->second << '\n';
  std::cout << '\n';   
  return 0;
}
Output:

mymap contains using .begin:
a => 200
b => 100
c => 300
mymap contains using .rbegin:
c => 300
b => 100
a => 200
mymap contains using .cbegin:
a => 200
b => 100
c => 300

6. map member function to check the capacity.

empty : Test whether container is empty
size : Return container size
mabx_size : Return maximum size

7. map member function to access the elements.

operator[] : Access element
at   : Access element

#include <iostream>
#include <map>
//for more tutorials on C, C++, STL, DS visit www.ProDeveloperTutorial.com
using namespace std;

   
int main ()
{
  std::map<char,int> mymap;

  mymap['b'] = 100;
  mymap['a'] = 200;
  mymap['c'] = 300;
  mymap['d'] = 500;

  std::cout << "mymap['d'] is " << mymap['d'] << '\n';
  
   mymap.at('d') = 10;
   
  std::cout << "mymap['d'] is " << mymap['d'] << '\n';
   
  return 0;
}
Output:

mymap['d'] is 500
mymap['d'] is 10

8. map member function to modify the elements.

insert : Insert elements
erase : Erase elements
swap : Swap content
clear : Clear content

List Of Tutorials available in this website:

C Programming 20+ ChaptersC++ Programming 80+ Chapters
100+ Solved Coding QuestionsData Structures and Algorithms 85+ Chapters
System design 20+ ChaptersShell Scripting 12 Chapters
4g LTE 60+ ChaptersMost Frequently asked Coding questions
5G NR 50+ ChaptersLinux System Programming 20+ chapters
Share
Email
Tweet
Linkedin
Reddit
Stumble
Pinterest
Prev Article
Next Article

About The Author

prodevelopertutorial

Follow this blog to learn more about C, C++, Linux, Competitive Programming concepts, Data Structures.

ProDeveloperTutorial.com

Tutorials and Programming Solutions
Copyright © 2022 ProDeveloperTutorial.com