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

发布于 2024-12-21 23:04:41 字数 1657 浏览 2 评论 0原文

问题是:

在我的第一堂课中,我有一个向量、一个双精度变量,并且我重载了比较运算符。这是相关的代码:

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 ">"
};

在另一个类中,我有一个城市向量,每次满足条件时我都必须对其进行排序。为此,我有一个定义如下的结构:

编辑:(引用而不是值)

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

现在问题部分,当我对向量进行排序时,出现新的 City 对象,我无法解释原因:

编辑:

// 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());

编辑:(复制构造函数和赋值运算符)

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;
}

奇怪的是,只有当我按升序对 City 对象进行排序时才会发生这种情况,即如果我将 CitySortHelper 中的比较运算符从“<”更改为到“>”一切正常。

有什么想法为什么会发生这种情况吗?任何帮助表示赞赏。

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:

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, i.e. if I change the comparator operator in the CitySortHelper from "<" to ">" everything works fine.

Any ideas why this happens ?? Any help is appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

半仙 2024-12-28 23:04:41

CitySortHelper 需要通过 const 引用而不是值来获取参数。另一件需要记住的事情是,排序对 City 使用赋值运算符;检查您的赋值运算符是否正常工作。解决这两个问题应该可以解决问题。

CitySortHelper needs to take parameters by const reference, not by value. Another thing to keep in mind is that sort uses assignment operator for the City; check that your assignment operator is working correctly. Taking care of these two issues should fix the problem.

腹黑女流氓 2024-12-28 23:04:41

将您的排序助手更改为具有

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

并且还要检查 City 复制构造函数和赋值运算符是否执行正确的操作

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

独﹏钓一江月 2024-12-28 23:04:41

如果您想保留顺序,则不应使用 std::sort(),而应使用 std::stable_sort()stable_sort 保证元素保持其相对顺序,而 sort 则不然。

另外,似乎 sort 不是你的问题。似乎有 City 对象被推入向量中的某个地方,而您没有注意到它们,因为您正在检查变量的大小而不是向量的迭代器。尝试像这样打印并告诉我们结果:

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

You shouldn't use std::sort() if you want to preserve order, you should use std::stable_sort(). stable_sort guarantees elements maintain their relative order, sort doesn't.

Also, it doesn't seem like sort is your problem here. 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. 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;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文