【问题标题】:How to sort a vector<pair<string , pair<int , int>>>?如何对向量<pair<string、pair<int、int>>> 进行排序?
【发布时间】:2013-11-22 21:56:43
【问题描述】:

我希望能够对以下向量进行排序 - vector> > 基于 pair 的第一个元素,如果它们相等,则根据它们的第二个元素对它们进行排序,我如何使用 STL 在 C++ 中做到这一点构造?

这个排序必须完成一些事情

假设 E1 和 E2 是 2 个元素

如果 E1.second.first == E2.second.first 则必须针对第二个元素进行比较。

【问题讨论】:

    标签: c++ sorting stl


    【解决方案1】:

    如果您不能使用 C++11 功能,您仍然可以执行以下操作:

    typedef std::pair<std::string, std::pair<int, int>> AnkitSablok;
    
    struct my_compare {
        bool operator()(const AnkitSablok &lhs, const AnkitSablok &rhs) const {
            return lhs.second < rhs.second;
        }
    };
    
    int main()
    {
        std::vector<AnkitSablok> vec;
    
        std::sort(vec.begin(), vec.end(), my_compare());
    }
    

    【讨论】:

    • 它成功了 :D ,感谢blastfurnace 这么棒的回复 :D ,谢谢你,总的来说,你能解释我如何完成这些工作吗?一些理论会很有用:)
    • @EvgenyPanasyuk:谢谢,我已经有一段时间没有使用严格的 C++03 编译器了。
    • 简单整洁!只是一个建议,既然你反正超载(),为什么不超载operator&lt;。不需要仿函数,排序看起来更整洁std::sort(vec.begin(), vec.end());
    • @AnkitSablok:std::sort 采用可选的比较函数。这需要是一个可调用对象,例如函数指针、函数对象或 lambda。我只针对您的问题发布了一种可能的解决方案。
    【解决方案2】:

    [...] 基于对 的第一个元素,如果它们相等,则根据它们的第二个元素对它们进行排序[...]

    std::pair 已经有字典比较 C++03 20.2.2/6:

    template <class T1, class T2>
    bool operator<(const pair<T1, T2>& x, const pair<T1, T2>& y);
    
    Returns: x.first < y.first || (!(y.first < x.first) && x.second < y.second)
    

    因此,正如 WhozCraig 指出的那样,您应该只比较外对的 .seconds。

    这是一个 lambda 表达式,我没有 C++ 11,没有其他办法吗?

    使用函子:

    struct LessSecond
    {
        template<typename T, typename U>
        bool operator()(const std::pair<T,U> &x, const std::pair<T,U> &y) const
        {
            return x.second < y.second;
        }
    };
    // ...
    sort(x.begin(), x.end(), LessSecond());
    

    或者更通用的版本(取决于您的需要):

    struct LessSecondGeneric
    {
        template<typename Pair>
        bool operator()(const Pair &x, const Pair &y) const
        {
            return x.second < y.second;
        }
    };
    

    LIVE DEMO:

    #include <algorithm>
    #include <iostream>
    #include <iterator>
    #include <utility>
    #include <vector>
    
    struct LessSecond
    {
        template<typename T, typename U>
        bool operator()(const std::pair<T,U> &x, const std::pair<T,U> &y) const
        {
            return x.second < y.second;
        }
    };
    
    int main()
    {
        using namespace std;
    
        vector<pair<string , pair<int, int>>> x
        {
            {"1", {2, 1}}, {"2", {1, 1}}, {"3", {1, 2}}
        };
        sort(x.begin(), x.end(), LessSecond());
        for(const auto &p : x)
            cout << p.first << " (" << p.second.first << ", " << p.second.second << ")" << endl;
    }
    

    输出是:

    2 (1, 1)
    3 (1, 2)
    1 (2, 1)
    

    【讨论】:

    • 好的。那会让 OP 陷入昏迷,但我仍然喜欢它,特别是因为它是通用的。我正在倾倒我的答案。 +1
    【解决方案3】:
    sort(x.begin, x.end, [](const X & a, const X & b){return a.second.first < b.second.first ; }) ;
    

    其中 x 是您的容器,X 是元素的类型。

    【讨论】:

    • 你能解释一下你刚刚做了什么吗?
    • 另外,你能修正一下错别字吗? c:
    • 这是标准库中的排序函数 + 用作比较函数的 lambda 函数。
    • 在这种情况下 X 应该是什么 pair> ?
    猜你喜欢
    • 1970-01-01
    • 2020-08-03
    • 1970-01-01
    • 2011-10-29
    • 2020-10-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多