Search code examples
c++arraysbipartiteford-fulkerson

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


I am working on a max bipartite matching algorithim. I am having trouble figuring out how to set values in an array based on the key/value in the map.

Ultimately I need iterate through my rows, which correspond to my keys in the map mymap. And then I need to set the appropriate column to true (1) based on the value in mymap. Ultimately it would look something like this

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}
                      };

Currently my algorithim looks like this, you can see that I am stumped on how to iterate through the map to set the appropriate value in the array:

inline void keep_window_open() {char ch; cin>>ch;} // Driver program to test above functions

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;

// This code above works

    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;
}

Solution

  • You can't access the elements of a map using an index. You need to use the iterator instead. And in this case you can use the shorter "for each" style loop:

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

    You'll have to initialize the bpGraph array to false before executing this loop.