简体   繁体   English

C ++ std :: sort带有动态数组错误

[英]C++ std::sort with a dynamic array error

There are plenty of examples on how to use std::sort() with a std::vector . 关于如何将std::sort()std::vector结合使用,有很多示例。 For my specific homework assignment, I'm not allowed to use an std::vector , so I instead want to use std::sort() on a dynamic array of custom objects. 对于我的特定家庭作业,不允许使用std::vector ,因此我想在动态自定义对象数组上使用std::sort()

Like so: 像这样:

int numberOfRoads = 100000;
Road* roads = new Road[numberOfRoads];
// Assume the comparator is defined correctly (<- this was the problem)
std::sort(roads, roads + numberOfRoads, SortRoadsComparator);

And here is the main compiler error I receive: 这是我收到的主要编译器错误:

C:\Program Files (x86)\Microsoft Visual Studio 11.0\VC\include\algorithm(3781): error C2664: 'int (const void *,const void *)' : cannot convert parameter 1 from 'Road' to 'const void *'
1>          No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called

I receive this error about 20 times. 我收到此错误约20次。 What exactly is it requiring me to do? 我到底需要做什么?

SortRoadsComparator() SortRoadsComparator()

int SortRoadsComparator(void const *v1, void const *v2)
{
    Road *a = (Road *)v1;
    Road *b = (Road *)v2;

    if (a->Length > b->Length) 
        return 1;

    else if (b->Length > a->Length) 
        return -1;

    else
    {
        // Non-determinism case
        if (a->Length == b->Length)
        {
            if ( (min(a->CityA, a->CityB) < min(b->CityA, b->CityB)) ||
                 (
                      (min(a->CityA, a->CityB) == min(b->CityA, b->CityB)) && max(a->CityA, a->CityB) < max(b->CityA, b->CityB)                                   
                 )
               )
            {
                return -1;
            }
            else
            {
                return 1;
            }
        }
        else
        {
            // Not expecting this
        }
    }
}

Solved by billz's comment. 由Billz的评论解决。

It requires you to pass proper comparator into std::sort. 它要求您将适当的比较器传递给std :: sort。 If you read std::sort documentation you can see that it requires following signature: 如果您阅读std :: sort 文档 ,则可以看到它需要以下签名:

bool cmp(const Type1 &a, const Type2 &b);

and: 和:

The types Type1 and Type2 must be such that an object of type RandomIt can be dereferenced and then implicitly converted to both of them. Type1和Type2类型必须使得可以取消引用RandomIt类型的对象,然后将其隐式转换为它们两者。

So in your case dereferenced "iterator" roads has type Road& and function could be something like: 因此,在您的情况下,被取消引用的“迭代器”道路的类型为Road&,功能可能类似于:

bool cmp( const Road &a, const Road &b );

PS Looks like you are porting qsort code into C++. PS看起来您正在将qsort代码移植到C ++中。 Though suggestion to make signature to: 虽然建议签名:

int cmp( const Road &a, const Road &b );

will compile, it is logically incorrect and would hide the fact that you need to change returned values in your function and slightly change the logic. 会编译,这在逻辑上是不正确的,并且会掩盖一个事实,即您需要更改函数中的返回值并稍稍更改逻辑。 In current implementation std::sort will most probably crash, but definatelly will not sort your sequence the way you expect it to. 在当前的实现中,std :: sort很可能会崩溃,但是definatelly不会按照您期望的方式对序列进行排序。

SortRoadsComparator function prototype should be: SortRoadsComparator函数原型应为:

bool SortRoadsComparator(Road const& v1, Road const& v2);

You should make sure SortRoadsComparator returns weak ordered Road . 您应该确保SortRoadsComparator返回弱有序的Road

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM