Open In App
Related Articles

Map of pairs in STL

Improve Article
Improve
Save Article
Save
Like Article
Like

Map in STL is used to hash key and value. We generally see map being used for standard data types. We can also use map for pairs.

For example consider a simple problem, given a matrix and positions visited, print which positions are not visited.




// C++ program to demonstrate use of map
// for pairs
#include <bits/stdc++.h>
using namespace std;
  
map<pair<int, int>, int> vis;
  
// Print positions that are not marked
// as visited
void printPositions(int a[3][3])
{
    for (int i = 0; i < 3; i++)
        for (int j = 0; j < 3; j++)
            if (vis[{ i, j }] == 0)
                cout << "(" << i << ", " << j << ")"
                     << "\n";
}
  
int main()
{
    int mat[3][3] = { { 0, 1, 2 }, { 3, 4, 5 }, { 6, 7, 8 } };
  
    // Marking some positions as visited
    vis[{ 0, 0 }] = 1; // visit (0, 0)
    vis[{ 1, 0 }] = 1; // visit (1, 0)
    vis[{ 1, 1 }] = 1; // visit (1, 1)
    vis[{ 2, 2 }] = 1; // visit (2, 2)
  
    // print which positions in matrix are not visited by rat
    printPositions(mat);
    return 0;
}


Output:

(0, 1)
(0, 2)
(1, 2)
(2, 0)
(2, 1)

If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out - check it out now!

Last Updated : 18 Oct, 2017
Like Article
Save Article
Previous
Next
Similar Reads