📜  STL中的配对图

📅  最后修改于: 2021-05-30 18:46:23             🧑  作者: Mango

STL中的Map用于哈希键和值。我们通常会看到地图用于标准数据类型。我们也可以将地图用于配对。

例如,考虑一个简单的问题,给定一个矩阵和访问过的位置,打印不访问哪些位置。

// C++ program to demonstrate use of map
// for pairs
#include 
using namespace std;
  
map, 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;
}

输出:

(0, 1)
(0, 2)
(1, 2)
(2, 0)
(2, 1)
要从最佳影片策划和实践问题去学习,检查了C++基础课程为基础,以先进的C++和C++ STL课程基础加上STL。要完成从学习语言到DS Algo等的更多准备工作,请参阅“完整面试准备课程”