C++ 在其中一个上排序多变量结构';s变量
各位早上好,, 我试图根据其中一个变量的值对结构中连接的3个变量进行排序。为了清楚起见,我有一个结构化类型的变量,名为edge,它有3个整数:edge.a edge.b和edge.w。我想按edge上的值对edge进行排序。我发现要实现这一点,我需要使用布尔运算符,但我还没有发现如何使用布尔运算符。这是我的代码:C++ 在其中一个上排序多变量结构';s变量,c++,struct,quicksort,boolean-operations,C++,Struct,Quicksort,Boolean Operations,各位早上好,, 我试图根据其中一个变量的值对结构中连接的3个变量进行排序。为了清楚起见,我有一个结构化类型的变量,名为edge,它有3个整数:edge.a edge.b和edge.w。我想按edge上的值对edge进行排序。我发现要实现这一点,我需要使用布尔运算符,但我还没有发现如何使用布尔运算符。这是我的代码: struct type{ int a,b,w; bool operator<(const edge&A) const{ return w<
struct type{
int a,b,w;
bool operator<(const edge&A) const{
return w<A.w;
};
};
type edge[6];
sort (edge);
结构类型{
INTA,b,w;
bool操作符尝试以下操作
#include <algorithm>
//...
struct type{
int a,b,w;
bool operator<(const type& A) const{
return w<A.w;
};
};
type edge[6];
//...
std::sort( edge, edge + 6 );
#include <algorithm>
#include <iterator>
//...
struct type{
int a,b,w;
struct sort_by_a
{
bool operator ()(const type &lhs, const type &rhs ) const
{
return lhs.a < rhs.a;
}
};
struct sort_by_b
{
bool operator ()(const type &lhs, const type &rhs ) const
{
return lhs.b < rhs.b;
}
};
struct sort_by_w
{
bool operator ()(const type &lhs, const type &rhs ) const
{
return lhs.w < rhs.w;
}
};
};
type edge[6];
//...
std::sort( std::begin( edge ), std::end( edge ), type::sort_by_a() );
//...
std::sort( std::begin( edge ), std::end( edge ), type::sort_by_b() );
//...
std::sort( std::begin( edge ), std::end( edge ), type::sort_by_w() );
#包括
//...
结构类型{
INTA,b,w;
bool操作符是这个std::sort
,或者其他一些自定义的sort
函数吗?还有,你得到的当前输出是什么,它与你想要的有什么不同?你是在寻找std::sort(edge,edge+6);
,是std::sort吗?它是std::sort,我没有接受任何输出,它在sort(edge)上停止编译;我还没有尝试过排序(edge,edge+6)我回家后会尝试。Thnx btwThnx对于所有的帮助,我会在我的pc上访问时尝试所有这些,尽管我不知道如何使用迭代器:)Thnx很多它现在不会停止编译并运行…它不会给出任何输出,但我会找到答案!
#include <algorithm>
#include <iterator>
//...
struct type{
int a,b,w;
struct sort_by_a
{
bool operator ()(const type &lhs, const type &rhs ) const
{
return lhs.a < rhs.a;
}
};
struct sort_by_b
{
bool operator ()(const type &lhs, const type &rhs ) const
{
return lhs.b < rhs.b;
}
};
struct sort_by_w
{
bool operator ()(const type &lhs, const type &rhs ) const
{
return lhs.w < rhs.w;
}
};
};
type edge[6];
//...
std::sort( std::begin( edge ), std::end( edge ), type::sort_by_a() );
//...
std::sort( std::begin( edge ), std::end( edge ), type::sort_by_b() );
//...
std::sort( std::begin( edge ), std::end( edge ), type::sort_by_w() );