将值从 Map 复制到 C++ 程序中的 256x256 数组

Copying values from a Map to a 256x256 array in a C++ program

我正在研究最大二分匹配算法。我无法弄清楚如何根据地图中的 key/value 在数组中设置值。

最终我需要遍历我的行,这些行对应于我在地图 mymap 中的键。然后我需要根据 mymap 中的值将适当的列设置为 true (1)。最终它看起来像这样

bool bpGraph[V][V] = { {0, 0, 0, 1, 0, ect.... 0}, //Key 1 Value 4
                        {0, 2, 0, 0, 0, ect.... 0}, // Key 2 Value 2
                        {0, 0},
                        {0, 0},
                        {0, 0},
                        {0, 0},
                        {0, 0}
                      };

目前我的算法看起来像这样,你可以看到我对如何遍历地图以在数组中设置适当的值感到困惑:

内联无效keep_window_open() {char ch; cin>>ch;} // 测试以上功能的驱动程序

int main()
{

    ifstream myfile("H:\School\CSC-718\paths.txt"); 
    std::map<int, int> mymap; // Create the map
    pair<int,int> me; // Define the pair
    char commas; // Address the comma in the file
    int a, b;
    vector<int> v;

    while (myfile >> a >> commas >> b)
    {
    mymap[a] = b; // Transfer ints to the map
    }
    mymap;

// 上面的代码有效

    bool bpGraph[M][N]; // Define my graph array

  for (int i = 0; i <mymap.count; i++) // the plan is to iterate through the keys M
 and set the appropriate value true on N 
  {

    bool bpGraph[M][N] = { 

    };
  }
    cout << "Maximum number networks "
         << maxBPM(bpGraph); // run it through the BPM algorithim
    keep_window_open();
    return 0;
}

您无法使用索引访问地图的元素。您需要改用迭代器。在这种情况下,您可以使用更短的 "for each" 样式循环:

for (const auto &val: mymap) {
    bpGraph[val.first][val.second] = true;
}

在执行此循环之前,您必须将 bpGraph 数组初始化为 false。