Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/133.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ C++;如何对向量进行排序<;类别*>;与操作员<;_C++_Sorting_Vector - Fatal编程技术网

C++ C++;如何对向量进行排序<;类别*>;与操作员<;

C++ C++;如何对向量进行排序<;类别*>;与操作员<;,c++,sorting,vector,C++,Sorting,Vector,我有 c1类{ 公众: 整数; c1() { number=rand()%10; } 布尔运算符

我有

c1类{
公众:
整数;
c1()
{
number=rand()%10;
}
布尔运算符<(c1*w)
{
返回编号number;
}
};
向量向量={…}
排序(vec.begin(),vec.end())
为什么会这样

但是如果我们有

 class c1{

public:
    int number;

    c1()
    {
        number=rand()%10;
    }

    bool operator < (c1 *w)
    {
        return number < w->number;
    }


};

vector<c1*> vec = { ... }
sort(vec.begin(),vec.end()) 
bool运算符<(c1 w)
{
返回编号

向量向量={…}

它会被分类的

bool操作符(c1*w)
c1
c1*
进行比较-您的排序将
c1*
c1*
回答标题问题,您不能

指针是内置类型,不能重写所有操作数都是内置类型的运算符


幸运的是,有一个重载
std::sort
,允许您指定一个比较函数(或functor),以便
操作符添加一个外部操作符<并保留原始操作符:

vector<c1> vec = { ... }

Phimueme的回答总结了这一点,我只想补充一点,作为一种解决方法,您可以创建一个只包含一个成员的包装类—一个指向c1的指针,然后重载其运算符,您需要向
std::sort
传递一个比较函数:

bool operator<(c1* a, c1* b) { return *a < *b; } 
bool比较_c1(c1*x,c1*y)
{
返回*x
或者,如果您使用的是GCC>=4.5或Visual Studio 2010(我不确定英特尔编译器),则可以使用lambdas(它们是C++0x标准的一部分):

排序(v.begin(),v.end(),[](c1*x,c1*y){return*x
最简单的方法是定义函数

std::sort(v.begin(), v.end(), [] (c1* x, c1* y) { return *x < y; });
bool c1\u ptr\u less(c1常量*lhs,c1常量*rhs){
返回左驾->某物<右驾->某物;
}
std::sort(vec.begin()、vec.end()、和c1_ptr_less);
我建议使用一个通用函子来处理所有指针数组

bool c1_ptr_less( c1 const *lhs, c1 const *rhs ) {
    return lhs->something < rhs->something;
}

std::sort( vec.begin(), vec.end(), & c1_ptr_less );
struct pointer\u less{
模板
布尔运算符()(T常量*lhs,T常量*rhs)常量
{return*lhs<*rhs;}
};
排序(vec.begin(),vec.end(),pointer_less());
有了它,就可以定义通常的
c1::operator<(const c1&)
,对于其他类也是如此


通常,最佳做法是完全避免使用指针,包括指针数组。

在您的示例中,
向量
被排序。只是不去看电影 您似乎想要的条件:默认情况下,
sort
使用
std::less
作为订购标准,以及
std::less
比较指针(这是您所期望的)。如果你 如果不想要默认条件,则必须通过第三个条件
排序
的参数,该谓词定义所需的排序


当然,您的成员
operatorSidenote:假设c1存储的不仅仅是一个int,您可能不希望通过值传递c1实例(如bool operator<(c1w)),因为这将导致在比较之前进行复制。而是通过引用传递参数(布尔运算符<(常量c1&w))。请注意,这不会解决您原来的问题(请参阅其他答案),只是一个一般性的说明。请看一下Boost中的指针容器库,它似乎提供了处理指针序列的方法,就像处理值序列一样。正如问题中所述,我将更改bool运算符-1:这不会编译。如果两个输入都是基元类型,则不能定义运算符重载。@只能定义nit,但指针不是基元类型。规则是重载运算符的至少一种类型必须是类类型或枚举类型,或者是对类类型或枚举类型的引用。(但我们知道你的意思:-)你的意思是
return*x<*y?@Rob Adams,不,主题启动程序的代码包含
c1::operator<(c1*)
,我正在使用它。
std::sort(v.begin(), v.end(), [] (c1* x, c1* y) { return *x < y; });
bool c1_ptr_less( c1 const *lhs, c1 const *rhs ) {
    return lhs->something < rhs->something;
}

std::sort( vec.begin(), vec.end(), & c1_ptr_less );
struct pointer_less {
    template< typename T >
    bool operator()( T const *lhs, T const *rhs ) const
        { return * lhs < * rhs; }
};

std::sort( vec.begin(), vec.end(), pointer_less() );