-4

我正在尝试创建存储在向量内的图形边缘的组合。我还需要生成并将生成的向量存储在向量中。以下是我到目前为止所做的,但目前尚未编译;

#include <stdlib.h>
#include<iostream>
#include<vector>
#include<algorithm>

struct edge{

int a;
int b;
int weight;
edge(int u,int v,int cost)
{
    a=u;
    b=v;
    weight=cost;
}

};


int main()
 {
  typedef std::vector<edge> V; //<or_any_class>
  V v;

    v.push_back(1,2,10);
    v.push_back(1,3,10);
    v.push_back(1,4,10);
    v.push_back(3,4,10);
    v.push_back(3,5,10);
    v.push_back(3,5,10);

  do{
        std::cout<<v[0]<<" "<<v[1]<<" "<<v[2]<<" "<<v[3]<<" "<<v[4]<<std::endl;
    }

   while(std::next_permutation(v.begin(),v.end()));
return 0;
 }

What Im trying to get as output;

 1 2
 1 3
 1 4
 3 4
 3 5
 4 3
 1 2 1 3
 1 2 1 4

有什么提示吗?

4

2 回答 2

7

错误一:

 v.push_back(1,2,10);

应该

 v.push_back(edge(1,2,10));
             ^^^^

错误二:

您需要operator <<为您的edge类定义以便std::cout<<v[0]可以编译

std::ostream& operator << (std::ostream& o, const edge& e)
{
     return o << e.a << " " << e.b << " " << e.weight;
}

错误三:

您需要operator <为您的egde类定义,以便std::next_permutation(v.begin(),v.end())可以编译和工作

bool operator < (const edge& e1, const edge& e2)
{
    if(e1.a != e2.a) return e1.a < e2.a;
    if(e1.b != e2.b) return e1.b < e2.b;
    return e1.weight < e2.weight; 
}
于 2012-06-27T13:08:11.073 回答
3

这个:

v.push_back(1,2,10); 

应该:

v.push_back(edge(1,2,10)); 
于 2012-06-27T13:08:46.610 回答