簡體   English   中英

使用std :: sort對包含兩個數據成員的對象的向量進行排序

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

我試圖將文件中的數據存儲到對象的向量中,然后對數據成員進行排序,但出現錯誤“無法確定要使用哪個重載函數“ sort”的實例”。 我嘗試將lambda與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;
}

您的代碼中有幾個問題。

不應 using namespace std; 避免名稱沖突。

而且,很不幸,您的time (小寫)類與另一個time標准標識符沖突。 只需使用Uppercase約定來命名類。

另外,您可能希望將Time::getHour()Time::getMinute()方法標記為const ,因為它們不會修改Time對象的內部狀態。

您還會有一個帶sort調用的錯字 ,因為您在sort后面有一個點。

而且,在C ++ 11/14中,我建議您使用基於范圍的for循環,而不要使用整數索引的顯式for 循環

考慮到這些方面,我已經對您的代碼進行了一些重構,並且現在可以使用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類的實例進行排序,則可以簡單地調用std::sort()而不使用任何自定義比較器,並且自定義的operator<實現將由編譯器自動獲取

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()用於operator<實現( 在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);
}   

};

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM