需要 STL 排序算法帮助

发布于 2024-07-26 18:26:25 字数 1257 浏览 2 评论 0原文

我在这里使用 std::sort 算法时遇到一些麻烦。 我读到您可以重载小于运算符来对类进行排序,但我收到了各种错误。 我还尝试使用函子,正如您在下面的示例中看到的那样。

我希望有人能看到我在这里做错了什么。

#include <iostream>
#include <vector>
#include <algorithm>

#include <stdlib.h>
#include <time.h>

class Thing {
public:
    Thing(int val) {
        this->_val = val;
    }

    bool operator<(Thing& rhs) {
        std::cout << "this works!";
        return this->val() < rhs.val();
    }

    int val() {
        return this->_val;
    }
protected:
    int _val;
};

struct Sort {
    bool operator()(Thing& start, Thing& end) {
        return start.val() < end.val();
    }
};

int main (int argc, char * const argv[]) {
    std::srand(std::time(NULL));

    std::vector<Thing> things;
    for(int i = 0; i < 100; i++) {
        Thing myThing(std::rand());
        things.push_back(myThing);
    }

    if(things[1] < things[2]) {
        //This works
    }

    //std::sort(things.begin(), things.end()); //This doesn't

    //std::sort(things.begin(), things.end(), Sort()); //Neither does this

    for(int i = 0; i < 100; i++) {
        std::cout << things.at(i).val() << std::endl;
    }

    return 0;
}

I'm having some troubles with using the std::sort algorithm here. I was reading that you can just overload the less than operator to sort classes, but I have been getting all sorts of errors. I have also tried using a functor as you can see in the example I made below.

I was hoping somebody could see what I'm doing wrong here.

#include <iostream>
#include <vector>
#include <algorithm>

#include <stdlib.h>
#include <time.h>

class Thing {
public:
    Thing(int val) {
        this->_val = val;
    }

    bool operator<(Thing& rhs) {
        std::cout << "this works!";
        return this->val() < rhs.val();
    }

    int val() {
        return this->_val;
    }
protected:
    int _val;
};

struct Sort {
    bool operator()(Thing& start, Thing& end) {
        return start.val() < end.val();
    }
};

int main (int argc, char * const argv[]) {
    std::srand(std::time(NULL));

    std::vector<Thing> things;
    for(int i = 0; i < 100; i++) {
        Thing myThing(std::rand());
        things.push_back(myThing);
    }

    if(things[1] < things[2]) {
        //This works
    }

    //std::sort(things.begin(), things.end()); //This doesn't

    //std::sort(things.begin(), things.end(), Sort()); //Neither does this

    for(int i = 0; i < 100; i++) {
        std::cout << things.at(i).val() << std::endl;
    }

    return 0;
}

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

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

发布评论

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

评论(3

亢潮 2024-08-02 18:26:25

创建您的 val()operator<() const 函数。

Sort::operator() 也是如此 — 采用 const Thing& 而不是 Thing&

Make your val() and operator<() const functions.

The same for Sort::operator() — take const Thing& instead of Thing&.

樱桃奶球 2024-08-02 18:26:25

我相信你需要改变

bool operator()(Thing& start, Thing& end) {

bool operator()(const Thing& start, const Thing& end) {

IOW

int val() {

,你的代码需要是常量正确的,并且不能

int val() const {

声称它可以修改它实际上没有(也不需要)的东西。

I believe you need to change

bool operator()(Thing& start, Thing& end) {

into

bool operator()(const Thing& start, const Thing& end) {

and

int val() {

into

int val() const {

IOW, your code needs to be const-correct and not claim it may modify things it in fact doesn't (nor needs to).

剑心龙吟 2024-08-02 18:26:25

尝试使运算符< 通过 const 引用获取其参数。 执行此操作时,您需要更改其实现以直接访问 _val 或(最好)将 val() 设置为 const(因为 const 成员函数无法调用非常量成员函数)。

Try making operator< take its argument by const reference. You'll need to change its implementation to directly access _val or (preferably) make val() const as well when you do this (because a const member function can't call a non-const one).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文