我正在尝试创建一个存储在矢量中的图形边缘组合.我还需要在向量中生成AND存储生成的向量.以下是我到目前为止所做的工作,但目前尚未编制;
#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
Run Code Online (Sandbox Code Playgroud)
任何提示?
错误一:
v.push_back(1,2,10);
Run Code Online (Sandbox Code Playgroud)
应该
v.push_back(edge(1,2,10));
^^^^
Run Code Online (Sandbox Code Playgroud)
错误二:
您需要operator <<为您的edge类定义以便std::cout<<v[0]可以编译
std::ostream& operator << (std::ostream& o, const edge& e)
{
return o << e.a << " " << e.b << " " << e.weight;
}
Run Code Online (Sandbox Code Playgroud)
错误三:
您需要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;
}
Run Code Online (Sandbox Code Playgroud)