loops - Taking Int values out of a map and iterating them into a bipartite graph C++ -


this complex me, not others.

i have set of 256 key/value items in map. got data map text file via ifstream. now, need take key/value pairs map , need use these data points create bipartite mapping graph.

what need is iterate through each key , value , put them inside bpgraph in format:

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

essentially through map , set 256 values in 256x256 array , set right value true key being horizontal row , value being vertical row.

this code have currently:

int main(){      ifstream myfile("h:\\school\\csc-718\\paths.txt");      std::map<int, int> mymap;     pair<int,int> me;     char commas;     int a, b;     vector<int> v;      while (myfile >> >> commas >> b){         mymap[a] = b;     }     mymap;      bool bpgraph[m][n] = {         //...     };      cout << "maximum number networks "          << maxbpm(bpgraph);      return 0; } 

question:

could have advice/suggestions regarding implementation?

you can iterate map using iterator. iterator can extract key , value using 'first' , 'second' members respectively. can put these values in array.

for following code, iterates map , prints each key , associated value:

void mapexample(void) {     std::map<int, int> mymap;      mymap[1] = 11;     mymap[2] = 22;     mymap[3] = 33;      (std::map<int,int>::iterator it=mymap.begin();it!=mymap.end(); ++it)     {         std::cout << it->first << "->" << it->second << "\n";     } } 

the above prints

1->11

2->22

3->33

edit: not sure if understand correctly, if do, want build equivalent representation of data, uses 256x256 array instead of map.

so iterating map do:

    (std::map<int,int>::iterator it=mymap.begin();it!=mymap.end(); ++it)     {          int key = it->first;          int value = it->second;           bpgraph[key-1][value-1] = 1;  //the indexes array 0 based, need subtract 1, if keys , values belong {1, ... ,256}     } 

note need initialize array elements zero. after execution of loop, v[i][j]=1 if key (i+1) associated value (j+1).


Comments

Popular posts from this blog

html - Outlook 2010 Anchor (url/address/link) -

javascript - Why does running this loop 9 times take 100x longer than running it 8 times? -

Getting gateway time-out Rails app with Nginx + Puma running on Digital Ocean -