在c++程序中将Map中的值复制到256x256数组

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

本文关键字:复制 256x256 数组 Map c++ 程序      更新时间:2023-10-16

我正在研究一个最大二部匹配算法。我有麻烦弄清楚如何设置值在一个数组基于键/值在地图。

最后我需要遍历行,这些行对应于映射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}
                      };

目前我的算法是这样的,你可以看到我被如何遍历映射以在数组中设置适当的值难住了:

inline void keep_window_open() {char};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;
}

不能使用索引访问map的元素。您需要使用迭代器。在这种情况下,你可以使用更短的"for each"样式循环:

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

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