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.
#include <bits/stdc++.h>
using namespace std;
map<pair< int , int >, int > vis;
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 } };
vis[{ 0, 0 }] = 1;
vis[{ 1, 0 }] = 1;
vis[{ 1, 1 }] = 1;
vis[{ 2, 2 }] = 1;
printPositions(mat);
return 0;
}
|
Output:
(0, 1)
(0, 2)
(1, 2)
(2, 0)
(2, 1)
Last Updated :
18 Oct, 2017
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...