简体   繁体   English

如何根据向量的值的属性对其进行排序?

[英]How can I sort a vector according to a property of its values?

If I have a vector of pairs 如果我有一对向量

vector<pair<int,vector<int>>> myvector;

How can I sort this vector according to nth element of its inner vector? 如何根据其内部向量的第n个元素对该向量排序?

My previously-asked question on this topic contains the answer of sorting a vector of pairs according to a second element of the pair by using a pre-defined sorting_function() as below. 先前在该主题上提出的问题包含使用以下预定义的sorting_function()根据对的第二个元素对对向量进行排序的答案。 However, in this case, I have no idea what the form of sorting_function() should be... 但是,在这种情况下,我不知道sorting_function()的形式应为...

sort(v.begin(), v.end(), sorting_function());

You may re-use the comparator from my answer to your previous question; 您可以根据我对上一个问题的回答重新使用比较器; sort expects an instance of it, so pass in a temporary one: sort需要一个实例 ,因此传入一个临时实例

std::sort(v.begin(), v.end(), MyComparator<1>());

So, here's the full example: 因此,这是完整的示例:

template <std::size_t N>
struct MyComparator
{
   typedef std::pair<int, std::vector<int>> value_type;
   bool operator()(const value_type& lhs, const value_type& rhs)
   {
      return lhs.second.at(N) < rhs.second.at(N);
   }
};

/**
 * A set of (int, int{2,}) pairs, sorted by the 2nd element in
 * the 2nd item of each pair.
 */
std::vector<std::pair<int, std::vector<int>>> my_data;

int main()
{
    my_data.push_back(std::make_pair(1, std::vector<int>{0,5,0,0}));
    my_data.push_back(std::make_pair(2, std::vector<int>{0,2,0,0}));
    my_data.push_back(std::make_pair(3, std::vector<int>{0,1,0,0}));
    my_data.push_back(std::make_pair(4, std::vector<int>{0,9,0,0}));

    std::sort(my_data.begin(), my_data.end(), MyComparator<1>());

    for (const auto& el : my_data)
        std::cout << el.first << ' ';
}

// Output: 3 2 1 4

( live demo ) 现场演示

struct comparator
{
    comparator (int n) : n(n) { }

    typedef std::pair<int, std::vector<int>> key_type;

    bool operator() (key_type const & k1, key_type const & k2)
    {
        return k1.second[n] < k2.second[n];
    }

    int n;
};

I have no idea where you want 'n' to come from, but basicly that's it. 我不知道您想从哪里来,但是基本上就是这样。 Also you should put bounds checking in it. 另外,您还应该在其中检查范围。

Then you can sort your array this way: 然后,您可以通过以下方式对数组进行排序:

std::sort(v.begin(), v.end(), comparator(n));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何根据地图值的属性对地图进行排序? - How can I sort a map according to a property of its values? 如何使用 std::sort() 对指向值的向量进行排序? - How can can I sort a vector of pointed-to values using std::sort()? 如何根据另一个向量的排序对向量进行排序? - How can I sort a vector based on sort of another vector? 如何按不同 std::vector 的值对 std::vector 进行排序? - How do I sort a std::vector by the values of a different std::vector? INFO解析器,属性树-如何根据单个键收集所有值 - INFO parser, property tree - how can I gather all values according to the single key 如何使用快速排序对二维双打向量进行排序? - How can I sort a 2D vector of doubles with Quick Sort? 如何根据第一个向量的变化对双向量排序? - How sort double vector according to changes in first vector? 如何根据一个成员的值对结构数组进行排序,并在另一个成员的基础上打破关系? - How to sort an array of structures according to values of one of its members, breaking ties on the basis of another member? 如何根据声明顺序对方法实现进行排序 - How can I sort method implementations according to their declaration order 如何根据标题长度调整 QDialog 的大小? - How can I adjust the size of a QDialog according to its title length?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM