checking if given vector contains already elements of pair in c++ -
let suppose have following pairs
0 -1 0 -2 0 -3 1- 2 2 -3 3- 4 2 -4 4- 5 5 -6
i insert pairs vector , should have each element 1 times , instance start empty vector :
0-1 inserted checking 0-2 , there exist 0, not 2, 0-2 inserted, have
0-1 0-2
now 0-3 , 3 not in list, can insert
0-1 0-2 0-3
now lets consider 1-2 , of course have both of them , skip, let consider 2-3, again can skip, 3-4 , 3 exist not 4, can insert 3-4 , after inserting of 4 , 4 exist,so reject 2-4 comes 4-5 , 5-6, have following list
0-1 0-2 0-3 3-4 4-5 5-6
i have following code
#include<iostream> #include<vector> #include<set> #include<algorithm> using namespace std; struct edge { int a, c; float weight;//edge a-c has weight bool operator() (edge x, edge y) { x.weight < y.weight; } }; int noncyclical_sum(vector<edge>s) { int total = 0; std::vector<std::pair<int, int>> b; auto m = make_pair(s[0].a, s[0].c); b.push_back(m); total = total + s[0].weight; vector<edge>::iterator it; (int = 1; < s.size(); i++) { auto m = make_pair(s[i].a, s[i].c); //if (find(b.begin(), b.end(), s[i].a) != b.end() && find(b.begin(), b.end(), s[i].c) != b.end()) if (find(b.begin(), b.end(), m.first) != b.end() && find(b.begin(), b.end(), m.second) != b.end()) { continue; //both element in vector } else { b.push_back(m); total = total + s[i].weight; } std::vector<std::pair<int, int>>::iterator ii; (ii = b.begin(); ii != b.end(); ii++) cout << ii->first << " " << ii->second; } } int main() { return 0; }
at first time , have pushed first pair, starting second one, checking if @ same time both element in vector, rejecting pairs , continue, otherwise push new pairs , continuing , have following error
severity code description project file line suppression state error c2678 binary '==': no operator found takes left-hand operand of type 'std::pair<int,int>' (or there no acceptable conversion) kurskal_algorithm c:\program files (x86)\microsoft visual studio\2017\enterprise\vc\tools\msvc\14.10.25017\include\xutility 3161
what wrong ? in advance
the problem in line:
if (find(b.begin(), b.end(), m.first) != b.end() && find(b.begin(), b.end(), m.second) != b.end())
let's check arguments of std::find
call: b.begin()
, b.end()
std::vector<std::pair<int, int>>::iterator
s while m.first
int
.
so, you're trying find int
in vector
of pair
s. can't that.
also, of functions lack required return
statements.
Comments
Post a Comment