Compare vector of pair! | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Compare vector of pair!

I have a vector of pair i.e., vector<pair<int,int>>graph; which has data like this: 1 0 2 1 1 2 3 2 I want to make adjacency list like : ( 1, 0) -> ( 2, 1) ( 1, 2) ( 2, 1) -> ( 1, 0) ( 1, 2) ( 1, 2) -> ( 1, 0) ( 2, 1) ( 1, 2) ( 3, 2) -> ( 2, 1) (1, 2) If one of the element of pair is a part of second pair. It should be a part of adjacency list. I tried this, vector<pair<int,int>>graph2(totalnode); while (input >> node1 >> node2) { graph2.push_back(make_pair(node1,node2)); } vector<pair<int,int>>::iterator firstloop; vector<pair<int, int>>::iterator secondloop; for(firstloop = graph2.begin();firstloop != graph2.end(); firstloop++) { for (secondloop = graph2.begin(); secondloop != graph2.end(); secondloop++) { if (graph2[firstloop].first == graph2[secondloop].first || graph2[firstloop].first == graph2[secondloop].second || graph2[firstloop].second == graph2[secondloop].first || graph2[firstloop].second == graph2[secondloop].second) { } } } But this is not right, what can I do for that. And once I found the matching element, where do I save it ( in another vector of pair?).

9th Jan 2019, 7:38 PM
Akhil Arya
Akhil Arya - avatar
1 Answer
0
How is this a vector/list? Looks like a map of pair to list of pairs or std::multimap. Is (1, 0) mapped to a list of (2, 1) (1, 2)? Why not use a matrix?
10th Jan 2019, 7:07 AM
Claude Martin
Claude Martin - avatar