使用 std::sort 对包含两个数据成员的对象向量进行排序

Sorting a vector of objects containing two data members using std::sort

我试图将文件中的数据存储到对象向量中,然后对数据成员进行排序,但出现错误 "Cannot determine which instance of overloaded function "sort" is intended"。我试过将 lambdas 与 sort 一起使用,还认为这可能是我创建比较函数的方式(是 a.hour > b.hour 还是我应该使用 a.getHour() 和 b.getHour()?) 我实际上想按小时和分钟对向量进行排序,但首先只在几个小时内对其进行测试似乎行不通。这是我目前所拥有的

#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <fstream>
using namespace std;

class time {
    int hour;
    int minute;
public:
    time(int h, int m) {
        hour = h;
        minute = m;
    }
    int getHour() { return hour; }
    int getMinute() { return minute; }
};

class Times {
    vector<time> t;
public:
    Times(string fileName) {
        //
    }

    void parse(ifstream& file) {
        //
        sort.(t.begin(), t.end(), lowerThan);
        //sort.(t.begin(), t.end(), [] (time& a, time& b) { return a.hour < b.hour; })
    }

    void display() {
        for (size_t i = 0; i < t.size(); i++) {
            cout << t[i].getHour() << ":" << t[i].getMinute() << endl;
        }
    }

    static bool lowerThan(time& a, time& b) { return a.getHour() < b.getHour(); }
};

int main() {
    Times times("File.txt");
    times.display();

    system("pause");
    return 0;
}

您的代码中存在几个问题。

should not use using namespace std; 避免名称冲突。

此外,很遗憾,您的 time(小写)class 与另一个 time 标准标识符冲突。只需使用 Uppercase 命名约定 classes.

此外,您可能希望将 Time::getHour()Time::getMinute() 方法标记为 const,因为它们不会修改Time 个对象的内部状态。

您还有一个 打字错误 调用 sort,因为您在 sort.

后面有一个点

并且,在 C++11/14 中,我建议您使用 基于范围的 for 循环 而不是使用整数的显式 for索引。

考虑到这些方面,我对您的代码进行了一些重构,it now works,同时使用了 lowerThan() 静态方法和 lambda。有空研究一下。

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

class Time {
    int hour;
    int minute;
public:
    Time(int h, int m) : hour(h), minute(m) {
    }

    int getHour() const { return hour; }
    int getMinute() const { return minute; }
};

class Times {
    std::vector<Time> t;

    static bool lowerThan(const Time& a, const Time& b) { 
        return a.getHour() < b.getHour(); 
    }

public:
    Times() {
        // Test data
        t.push_back(Time{10, 10});
        t.push_back(Time{9, 20});
        t.push_back(Time{8, 30});

        //std::sort(t.begin(), t.end(), lowerThan);

        std::sort(t.begin(), t.end(), [] (const Time& a, const Time& b) { 
            return a.getHour() < b.getHour(); 
        });
    }

    void display() {
        for (const auto& x : t) {
            std::cout << x.getHour() << ":" << x.getMinute() << '\n';
        }
    }
};

int main() {
    Times times;
    times.display();
}

另请注意,如果您定义自定义 operator< 重载来对 Time class 的实例进行排序,您可以简单地调用 std::sort() 而无需任何自定义比较器,并且operator< is automatically picked up by the compiler:

的自定义实现
class Time {
...
    friend bool operator<(const Time& a, const Time& b) {
        return a.getHour() < b.getHour();
        // ... or a more complete comparison, including minutes.
    }   
};

编辑

正如@DieterLücking 在评论中所建议的那样,您可以使用 std::tie() for the operator< implementation (live here on Ideone):

#include <tuple>  // for std::tie

class Time {
    ...
public:

friend bool operator<(const Time& a, const Time& b) {
    return std::tie(a.hour, a.minute) < std::tie(b.hour, b.minute);
}   

};