top button
Flag Notify
    Connect to us
      Facebook Login
      Site Registration

Facebook Login
Site Registration

How to use MAP STL in c++ programs?

+2 votes
315 views
How to use MAP STL in c++ programs?
posted Dec 26, 2015 by Rajan Paswan

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button
Following is simple program, which can help you?

#include <map>

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

  mymap[1]="abc";
  
  mymap[2]="xyz";
  
  for (auto& y: mymap) {
    std::cout << y.first << ": " << y.second << '\n';

}

1 Answer

0 votes

What is a map?

A map is a super-cool and useful method of storing data. It's kind of a cross between a vector and an array. It's just that cool.

How does a map work?

Basically, when we create a map, we give it two datatypes, as opposed to one (when we use a vector). The first is used as the key, which is basically used like when we use arrays. The second is the value, which is the actual value that this member will store.

In summary, a map is like an array, but think of the array syntax when giving a value to a part of an array:

grades[4] = 'A';

Rather than using a meaningless key, wouldn't it be cool if we could have something like:

grades["Danny"] = 'A';

Exactly! So that's basically what a map is. Now, onto the actual creation of one...

Including stuff - what needs to be included when we make a map?

#include <iostream>
#include <iomanip>
#include <map>

using namespace std;

Now, iostream and iomanip are only being included so that I can show you what's going on. In other words, all you really need to include is #include

(And don't forget that we need to be using std::map; too!)

What next? Well, I've written a function to display it using iterators

void display (map <string,char> grades)
{
    cout << "\tTotal size: " << grades.size() << endl; /* Output the size */
    /* Create an iterator, much like vector iterators */
    map <string, char>::iterator it;
    for (it = grades.begin(); it != grades.end(); ++it)
        /* Output first (which is the string) and second (which is the char) */
        cout << setw(10) << it->first << setw(5) << it->second << endl;

    cout << endl; /* Print a new line */
}

It looks a little confusing, but it should make sense by the end of this tutorial! Basically, I use setw() (from iomanip) to make the output tidy, it's nothing majorly important, just remember that first and second are important keywords! In this case, first refers to the string, and second refers to the grade.

Adding stuff to our map

This is relatively simple, really. Time for a chunk of code, heavily annotated explaining why maps are so awesome:

cout << "Populating map..." << endl;
map <string, char> grades; /* Create a map with string as a referencer and char as the storage */
grades["Danny"] = 'A'; /* This is how we add values to a map */
grades["Kayleigh"] = 'A'; /* Basically, it is like an array, but cooler */
grades["Joe"] = 'F'; /* We get to use any data type we like as the access key */
grades["Brad"] = 'C'; /* As opposed to using 'int' all the time wih an array */

cout << "Before any modifications:" << endl;
display (grades); /* Call my function to display the map */

Rather than using a highly unuseful index like grades[0] = 'A'; we've actually gone and given it a meaningful reference!

Deleting bits and bobs from a map

Super simple. Just erase() what you find() like so:

/* erase() will delete the member of the map that is being passed */
/* find() will find the data being stored under the key, and return the iterator position */
grades.erase (grades.find("Joe"));
cout << "After deleting a value:" << endl;
display (grades); /* Call my function to display the map */

Checking if something exists within a map

Again, this is really simple. Basically, we call count() to see if there's something in the map or not, like so:

/* Perform a couple of checks to see what is in the map */
if (grades.count("Danny")) /* if "Danny" is in the map */
     cout << "Danny is a member of grades" << endl;
else cout << "Danny is not a member of grades" << endl;
if (grades.count("Joe")) /* remember we deleted him! */
     cout << "Joe is a member of grades" << endl;
else cout << "Joe is not a member of grades" << endl;

Emptying a map

For those destructive people, we can delete everything stored in a map at once,

/* clear() does exactly what it says on the tin. It completely clears all data from the map */
grades.clear ();
cout << "After clearing the map:" << endl;
display (grades);

Don't forget to close off main! }

And there we have it! A simple demostration of how maps can be used. For those completists out there, here is the entire code which I used in this tutorial:

#include <iostream>
#include <iomanip>
#include <map>

using namespace std;

void display (map <string,char> grades)
{
    cout << "\tTotal size: " << grades.size() << endl; /* Output the size */
    /* Create an iterator, much like vector iterators */
    map <string, char>::iterator it;
    for (it = grades.begin(); it != grades.end(); ++it)
        /* Output first (which is the string) and second (which is the char) */
        cout << setw(10) << it->first << setw(5) << it->second << endl;

    cout << endl; /* Print a new line */
}

int main ()
{
    cout << "Populating map..." << endl;
    map <string, char> grades; /* Create a map with string as a referencer and char as the storage */
    grades["Danny"] = 'A'; /* This is how we add values to a map */
    grades["Kayleigh"] = 'A'; /* Basically, it is like an array, but cooler */
    grades["Joe"] = 'F'; /* We get to use any data type we like as the access key */
    grades["Brad"] = 'C'; /* As opposed to using 'int' all the time wih an array */

    cout << "Before any modifications:" << endl;
    display (grades); /* Call my function to display the map */

    /* erase() will delete the member of the map that is being passed */
    /* find() will find the data being stored under the key, and return the iterator position */
    grades.erase (grades.find("Joe"));
    cout << "After deleting a value:" << endl;
    display (grades); /* Call my function to display the map */

    /* Perform a couple of checks to see what is in the map */
    if (grades.count("Danny")) /* if "Danny" is in the map */
         cout << "Danny is a member of grades" << endl;
    else cout << "Danny is not a member of grades" << endl;
    if (grades.count("Joe")) /* remember we deleted him! */
         cout << "Joe is a member of grades" << endl;
    else cout << "Joe is not a member of grades" << endl;

    cout << endl; /* Add a bit of white space */

    /* clear() does exactly what it says on the tin. It completely clears all data from the map */
    grades.clear ();
    cout << "After clearing the map:" << endl;
    display (grades);

    cin.get ();
    return EXIT_SUCCESS;
}
answer May 24, 2016 by Manikandan J
Contact Us
+91 9880187415
sales@queryhome.net
support@queryhome.net
#280, 3rd floor, 5th Main
6th Sector, HSR Layout
Bangalore-560102
Karnataka INDIA.
QUERY HOME
...