DigestCPP

Lets Understand With Example

  • Home
  • Design Principal
  • Design Patterns
  • C++ 11 Features
  • C++11 Multithreading
  • Contact Us

std::map and std::multi map c++

map:

This container comes under Associative container, this is template class to store element of any type (in build as well as user defined data type). It stores the key and value (key-value pair) as a single element. There is NO duplicate key.

Header File:

#include<map>

Key Points:

  • It sorts their element automatically based on sorting criteria.
  • sorting will be done on key, check below example.
  • sorting criteria we can pass as argument while creating set object otherwise it will take default criteria that is less.
  • Internally its implemented as Balanced Binary tree.
  • There is NO push_back() and push_front() in it.

How to add element in map:

   //Create empty map object
    std::map<int, std::string> mp;

    //Add element using initializer list
    mp.insert({5, "aa"});
    //Add element using make_pair()
    mp.insert(std::make_pair(4, "cc"));
    //Add element using pair<>
    mp.insert(std::pair<int, std::string>(3, "bb"));
    //Add elememt using value type
    mp.insert(std::map<int, std::string>::value_type(1, "dd"));

How to find element in map:

    //find the key
    if(mp.find(9) != mp.end())
    {
        std::cout<<"Found"<<std::endl;
        //Process the data
    }
    else
    {
        std::cout<<"Not Found"<<std::endl;
    }

How to delete element in map:

    //Remove
    int num = mp.erase(4);
    std::cout<<"Remove count:"<<num<<std::endl;
    num = mp.erase(9);
    std::cout<<"Remove count:"<<num<<std::endl;

Source code of std::map with example:

/*
Program: STL map
Author: Alpha Master
Date: 17 September 2021
*/

//Header File
#include<iostream>
#include<map>

//Display function the show the content of map
void display(std::map<int, std::string>& mp)
{
    //using iterate to transverse the map and display the content
    std::map<int, std::string>::iterator itr;
    for(itr = mp.begin(); itr != mp.end(); itr++)
    {
        std::cout<<itr->first<<" "<<itr->second;
        std::cout<<std::endl;
    }
    std::cout<<std::endl;
}

int main()
{
    std::cout<<"STL MAP"<<std::endl;

   //Create empty map object
    std::map<int, std::string> mp;
    std::cout<<"size:"<<mp.size()<<std::endl;
 
    //Add element using initializer list
    mp.insert({5, "aa"});
    //Add element using make_pair()
    mp.insert(std::make_pair(4, "cc"));
    //Add element using pair<>
    mp.insert(std::pair<int, std::string>(3, "bb"));
    //Add elememt using value type
    mp.insert(std::map<int, std::string>::value_type(1, "dd"));
    mp.insert({2, "ee"});
    std::cout<<"size:"<<mp.size()<<std::endl;
    
    //display content
    display(mp);

    //find the key
    if(mp.find(4) != mp.end())
    {
        std::cout<<"Found"<<std::endl;
    }

    //find the key
    if(mp.find(9) != mp.end())
    {
        std::cout<<"Found"<<std::endl;
    }
    else
    {
        std::cout<<"Not Found"<<std::endl;
    }

    //Remove
    int num = mp.erase(4);
    std::cout<<"Remove count:"<<num<<std::endl;
    num = mp.erase(9);
    std::cout<<"Remove count:"<<num<<std::endl;
    
    //display content
    display(mp);


    return 0;
}

Output:

STL MAP
size:0
size:5
1 dd
2 ee
3 bb
4 cc
5 aa

Found
Not Found
Remove count:1
Remove count:0
1 dd
2 ee
3 bb
5 aa

Multi Map:

It allows duplicate key.

Primary Sidebar




DigestCPP © 2023. All rights reserved.

    About Privacy Policy Terms and Conditions Contact Us Disclaimer