CPP STL Tutorial : C++ std::map and it’s operations.

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.

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
max_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
Write a Comment

Leave a Comment

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