-1

let us suppose have following pairs

0 -1 
0 -2 
0 -3 
1- 2
2 -3
3- 4
2 -4
4- 5
5 -6

i would like to insert those pairs into vector , so that i should have each element only one times , for instance start with empty vector :

0-1 is inserted now we are checking 0-2 , there exist 0, but not 2, so 0-2 is inserted, we have

0-1
0-2

now 0-3 , 3 is not in the list, so we can insert

0-1 
0-2
0-3

now lets us consider 1-2 , of course we have both of them , so skip, now let us consider 2-3, again we can skip, 3-4 , 3 exist but not 4, so we can insert 3-4 , after inserting of 4 , 4 also exist,so reject 2-4 then comes 4-5 and 5-6, so we 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;
    for (int i = 1; i < 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 is in the  vector
        }
                else
        {

            b.push_back(m);
            total = total + s[i].weight;
                    }

        std::vector<std::pair<int, int>>::iterator ii;
        for (ii = b.begin(); ii != b.end(); ii++)
            cout << ii->first << "  " << ii->second;

            }

}
int main()
{



    return 0;
}

at first time , i have pushed first pair, starting from the second one, i am checking if at the same time both element is in vector, i am rejecting pairs and continue, otherwise i push new pairs and continuing , but i have following error

Severity    Code    Description Project File    Line    Suppression State
Error   C2678   binary '==': no operator found which takes a left-hand operand of type 'std::pair<int,int>' (or there is no acceptable conversion)  kurskal_algorithm   c:\program files (x86)\microsoft visual studio\2017\enterprise\vc\tools\msvc\14.10.25017\include\xutility   3161    

what is wrong ? thanks in advance

1 Answers1

3

The problem is in this line:

if (find(b.begin(), b.end(), m.first) != b.end() && find(b.begin(), b.end(), m.second) != b.end())

Let's check the arguments of std::find call: b.begin() and b.end() are std::vector<std::pair<int, int>>::iterators while m.first is int.

So, you're trying to find int in the vector of pairs. You can't do that.

Also, all of your functions lack the required return statements.

alexeykuzmin0
  • 6,344
  • 2
  • 28
  • 51