简体   繁体   English

为什么std :: sort()会改变排序的向量?

[英]Why does std::sort() change the sorted vector?

here is the problem: 这是问题所在:

In my first class i have a vector, a double variable and I overload the comparison operators. 在我的第一堂课中,我有一个向量,一个双变量,我重载比较运算符。 Here is the relevant code: 这是相关代码:

class City
{
    double distance;
    std::vector<int> coordinates;

    bool operator<(const City& city) const
    {   
        return this->distance < city.distance;
    } 

    // same for the greater-than operator but changing "<" to ">"
};

In another class I have a vector of cities, which I have to sort every time a condition is met. 在另一个班级,我有一个城市的向量,每次满足条件时我都要对它进行排序。 For that I have a struct defined as follows: 为此我有一个结构定义如下:

EDIT: (reference instead of value) 编辑:(参考而不是值)

struct CitySortHelper {
    bool operator() (const City &x, const City &y) const { return x < y; } 
} city_sort;

Now the problem part, when I sort the vector new City objects appear, and I can't explain why: 现在问题部分,当我对矢量新的City对象进行排序时,我无法解释原因:

EDIT: 编辑:

// this prints all current objects in the vector
for (int i = 0; i < totalCities; i++) {
    std::cout << cities->at(i) << std::endl;
}

// after the following line I get new City objects in the 
// vector, that weren't there before the sort. The new objects
// always have distance = 0 and random values in the coordinates
std::sort(cities->begin(), cities->end(), city_sort);

// using the sort with no predicate also gives the same faulty results
std::sort(cities->begin(), cities->end());

EDIT: (the copy constructor and assignment operator) 编辑:(复制构造函数和赋值运算符)

City(const City &city)
{
    this->distance = city.distance;
    this->coordinates = city.coordinates;
}

City& operator= (const City &city)
{
    this->distance = city.distance;
    this->coordinates = city.coordinates;

    return *this;
}

The weird part is that this only happens if I sort the City objects in ascending order, ie if I change the comparator operator in the CitySortHelper from "<" to ">" everything works fine. 奇怪的是,只有当我按照升序对City对象进行排序时才会发生这种情况,即如果我将CitySortHelper中的CitySortHelper从“<”更改为“>”,一切正常。

Any ideas why this happens ?? 任何想法为什么会这样? Any help is appreciated. 任何帮助表示赞赏。

CitySortHelper needs to take parameters by const reference, not by value. CitySortHelper需要通过const引用而不是值来获取参数。 Another thing to keep in mind is that sort uses assignment operator for the City ; 要记住的另一件事是sort使用City赋值运算符; check that your assignment operator is working correctly. 检查您的赋值运算符是否正常工作。 Taking care of these two issues should fix the problem. 处理这两个问题应该解决问题。

You shouldn't use std::sort() if you want to preserve order, you should use std::stable_sort() . 如果要保留顺序,则不应使用std::sort() ,应使用std::stable_sort() stable_sort guarantees elements maintain their relative order, sort doesn't. stable_sort保证元素保持其相对顺序, sort不保持。

Also, it doesn't seem like sort is your problem here. 此外,这似乎不是sort是你的问题。 It seems there are City objects getting pushed into the vector somewhere, and you aren't noticing them because you're checking on a variable for size instead of the vector's iterators. 似乎City对象被推入到某个地方的向量中,并且您没有注意到它们,因为您正在检查变量的大小而不是向量的迭代器。 Try printing like this instead and tell us what comes out: 尝试这样打印,并告诉我们出来的内容:

for (std::vector <City> ::iterator it = cities->begin(); it != cities->end(); ++it) {
    std::cout << *it << std::endl;
}

Change your sort helper to have 更改您的排序助手

bool operator() ( const City& x , const City& y) const

And also check that City copy constructor and assignment operator do the proper thing 并检查City复制构造函数和赋值运算符是否正确

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

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