简体   繁体   English

使用用户定义的比较类对std :: pair的std :: vector进行排序

[英]Sorting a std::vector of std::pair's using user defined compare class

I have two class templates MyClassA<T> and MyClassB<T> . 我有两个类模板MyClassA<T>MyClassB<T>

From these, I have constructed two std::vector 's as std::vector<MyClassA<double>> A and std::vector<MyClassB<double>> B . 从这些,我构造了两个std::vector作为std::vector<MyClassA<double>> Astd::vector<MyClassB<double>> B

My goal is to first sort A in ascending order (in actual I will do a range/partial sort). 我的目标是首先按升序对A进行排序(实际上,我将进行范围/部分排序)。

Then using that order to sort B . 然后使用该顺序对B进行排序。

What I am doing so far is the following: 到目前为止,我正在做以下事情:

#include <iostream>
#include <vector>
#include <algorithm>
#include <functional>
#include <random>

// my class definitions
template<typename T>
class MyClassA
{
public:
    T valA;
};

template<typename T>
class MyClassB
{
public:
    T valB;
};

// my compare class
template<typename T>
using TIter = typename std::vector<T>::const_iterator;

template <typename T>
class MyCompare
{
public:
    bool operator()(std::pair<std::size_t, TIter<MyClassA<T>>>
           const& a, std::pair<std::size_t, TIter<MyClassA<T>>> const& b)
    {
        return *(a.second).valA < *(b.second).valA;
    }
};

// sort from given order
//... not yet implemented

int main()
{
    std::random_device rd;
    std::mt19937 gen(rd());
    std::uniform_real_distribution<> dis(0, 1);

    // first ClassA Object vector A
    std::vector<MyClassA<double>> A(5);
    for(auto& i:A) i.valA = dis(gen);
    // second ClassB Object vector B
    std::vector<MyClassB<double>> B(5);
    for(auto& i:B) i.valB = dis(gen);

    // sort vector A elements' references in ascending order
    std::size_t i = 0;
    std::vector<std::pair<std::size_t, TIter<MyClassA<double>>>> torder(A.size());
    for(auto it = A.begin(); it != A.end(); ++it, ++i) torder[i] = std::make_pair(i, it);
    std::sort(torder.begin(), torder.end(), MyCompare<double>()); // getting error here

    // sort vectors A and B elements using the above sorted order
    // ...
    return 0;
}

However, I am getting the following error: 但是,我收到以下错误:

error: 'const class __gnu_cxx::__normal_iterator<const MyClassA<double>*, std::vector<MyClassA<double> > >' has no member named 'valA'

It's a simple case of problem with the operator precedence . 这是操作符优先级问题的简单例子。 The member selection dot . 成员选择点. has higher precedence than the dereference operator, so eg *(a.second).valA is parsed as *((a.second).valA) . 具有比取消引用运算符更高的优先级,因此,例如*(a.second).valA被解析为*((a.second).valA)

Simply change to eg a.second->valA (or (*a.second).valA ). 只需更改为例如a.second->valA (或(*a.second).valA )。

return *(a.second).valA < *(b.second).valA;

a.secondb.second似乎是迭代器,因此应该简单地是:

return (a.second)->valA < (b.second)->valA;

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

相关问题 对std :: vector进行排序 <std::pair<std::string,bool> &gt;由字符串? - Sorting a std::vector<std::pair<std::string,bool>> by the string? 在 std::vector 和 std::sort 中使用用户定义的类型 - Using user defined types with std::vector and std::sort 使用可以访问类成员的比较仿函数对std :: vector进行排序 - Sorting a std::vector with a compare functor which has access to class members 的std ::矢量 <std::pair<int,std::pair<Bone,std::string> &gt;&gt;不按int排序? - std::vector<std::pair<int,std::pair<Bone,std::string> > > not sorting by int? 在std :: vector中找到<std::pair> - find in std::vector<std::pair> 使用std :: sort在自定义类C ++中对向量进行排序 - Sorting a vector inside a custom class C++ using std::sort 使用std :: sort使用结构对对列表进行排序 - Sorting a list with struct pair using std::sort 使用lambda表达式作为比较std :: set,当它在一个向量中时 - Using lambda expression as Compare for std::set, when it's inside a vector 对类型的std :: vector进行排序的标准方法是什么 <std::pair<int, std::pair<int, int> &gt;&gt;根据第一对的“第一” - What is a standard way of sorting a std::vector of type <std::pair<int, std::pair<int, int>>> based on the “first” of the first pair 使用std :: transform来生成一对矢量 - Using std::transform to make a vector of pair
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM