Featured Post

Trie implementation in C

Usage of Map in C++

This is a simple implementation of usage of maps in C++ . Here I have used a simple map containing an int as the key and string as the value . This can further be extended to any data type needed by the use of templates.

Maps are like arrays used to store a pair of values together , each value identified by a key. So it becomes easier to search the values in a Map. One big advantage of maps is that they extend as we use them. Here the duplicate keys are not allowed , which means at a particular key value only one data would be stored. The new data is the one which stays in that particular key value , old one is overwritten .

We can use MultiMap if we need duplicates to be stored.


#include <iostream>
#include <string>
#include <map>
#include <limits>
#define BAD_INPUT_CUTOFF 3

using namespace std;

class MapDemo
{
private :
    map<int, string> mymap;
public:
    MapDemo();
    ~MapDemo();
    void insert_element();
    void delete_element();
    bool find(int);
    bool isEmpty();
    int mapsize();
    void display();
    void revdisplay();
    void clear();
};

unsigned int getInt(const char *prompt)
{
    int input, //used to get input
        bad_count=0; //used to keep track of how many bad attempts.
    do
    {
        std::cout << prompt;
        std::cin >> input;
        if (!std::cin.good())
        {
            std::cout << "\nInvalid Input! : Enter a number only..." << std::endl;
            std::cin.clear(); 

            //clear the buffer
            std::cin.ignore(std::numeric_limits<std::streamsize>::max(),'\n');

            bad_count++; //User made a bad input, count it
            input = 0;   // user did not enter a valid number
        }
    }
    while (input == 0 && bad_count < BAD_INPUT_CUTOFF);
    return input;
}

MapDemo :: MapDemo()
{
    cout<<"New Map created..."<<endl;
}

MapDemo :: ~MapDemo()
{
    cout<<"Map destroyed..."<<endl;
}

void MapDemo :: insert_element()
{
    int key;
    string value;

    key = getInt("\nEnter Key : ");
    
    if(find(key))
      cout<<"Warning : Duplicate Key!!!"<<endl;

    cout<<"\nEnter Value : ";
    cin>>value;
    
    mymap.insert(pair<int,string>(key, value));
}

void MapDemo :: delete_element()
{
  int key;
    if(!isEmpty())
    {
        key = getInt("\nEnter Key of the value to be deleted: ");
        if(find(key))
          mymap.erase(key);
        else
          cout<<"Key not Found !!! ";
    }
}

bool MapDemo :: find(int key)
{
    if(!mymap.empty())
    {
        if(mymap.find(key)->first != key )
        {
          return false;
        }
        else 
          return true;
    }
    return false;
}

bool MapDemo :: isEmpty()
{
    if(mymap.empty())
    {
        cout<<"Map is empty ! ! !"<<endl;
        return true;
    }
    else
        return false;
}

int MapDemo :: mapsize()
{
    return mymap.size();
}

void MapDemo :: display()
{
    map <int, string>::iterator itr;

    for(itr = mymap.begin(); itr != mymap.end(); itr++)
    {
        cout << (*itr).first <<" = "<<(*itr).second<<endl;
    }

}

void MapDemo :: revdisplay()
{
    map <int, string>::reverse_iterator ritr;
    cout << "Displaying in reverse.. "<<endl;
    for(ritr = mymap.rbegin(); ritr != mymap.rend(); ritr++)
    {
        cout << ritr->first <<" = "<<ritr->second<<endl;
    }

}

void MapDemo :: clear()
{
    mymap.clear();
}

int main()
{
    MapDemo m1, m2;

    m1.insert_element();
    m1.insert_element();
    m1.insert_element();
    m1.delete_element();
    m1.display();
    m1.revdisplay();
    m2.insert_element();
    m2.insert_element();
    m2.insert_element();
    m2.display();
    m2.clear();
    m2.delete_element();


}

Suggestions are Welcome... ;)

Comments

  1. Its map usage code as a member variable. Please understand the statement " This is a simple implementation of maps in C++ " properly

    ReplyDelete
    Replies
    1. Thanks for feedback. I have updated the text.

      Delete

Post a Comment

Please post your valuable suggestions