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

C++ 如何根据向量值的属性对向量进行排序?,c++,vector,C++,Vector,如果我有一对向量 vector<pair<int,vector<int>>> myvector; 你可以重复使用我对你上一个问题的回答中的比较器排序需要一个实例,因此传入一个临时实例: std::sort(v.begin(), v.end(), MyComparator<1>()); std::sort(v.begin()、v.end()、MyComparator()); 下面是一个完整的例子: template <std::size_

如果我有一对向量

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

你可以重复使用我对你上一个问题的回答中的比较器<代码>排序需要一个实例,因此传入一个临时实例:

std::sort(v.begin(), v.end(), MyComparator<1>());
std::sort(v.begin()、v.end()、MyComparator());
下面是一个完整的例子:

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
模板
结构比较器
{
typedef std::pair value_type;
布尔运算符()(常量值类型和lhs、常量值类型和rhs)
{
返回左秒(N)<右秒(N);
}
};
/**
*一组(int,int{2,})对,按中的第二个元素排序
*每对中的第二项。
*/
std::向量my_数据;
int main()
{
my_data.push_back(std::make_pair(1,std::vector{0,5,0,0}));
my_data.push_back(std::make_pair(2,std::vector{0,2,0,0}));
my_data.push_back(std::make_pair(3,std::vector{0,1,0,0}));
my_data.push_back(std::make_pair(4,std::vector{0,9,0,0}));
std::sort(my_data.begin()、my_data.end()、MyComparator());
用于(const auto&el:my_数据)

std::cout向量没有“键”因此,
键类型
是misnomer@LightnessRacesinOrbit键类型与向量无关,它是比较器的比较键类型好吧,我想这是有争议的。当你排序的是向量的元素类型时,它与向量有关,但你是对的,比较器不应该知道关于向量的任何事hat@lisyarus:std::sort(v.begin(),v.end(),comparator(n));第行给出一个错误:“需要3个参数-提供4个”。我该如何解决它呢?谢谢……谢谢,它似乎起作用了。但是,它仍然需要一个常量表达式。有没有办法为比较器设置一个变量?实际上,主要的问题是我需要用这样一个层次结构对外向量的值进行排序,这个层次结构需要扫描内向量的所有值。@user3744394:是的;转储模板te表示
MyComparator
并使
N
成为构造函数参数;然后:
MyComparator(1)这是相当基本的东西,你使用的是C++书?我正在网上学习。这是我的头几天,和类似的标准模板库的功能。@ USS344394:哦,不!从一本好书中学习。在线教程在质量和准确性上都有很大的不同,可以告诉你任何你都不聪明的东西。像这样改变它?struct MyComparator{int N;MyComparator(int initN){N=initN;}typedef std::pair value_type;bool operator()(const value_type&lhs,const value_type&rhs){返回lhs.second.at(N)>rhs.second.at(N);};
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
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;
};
std::sort(v.begin(), v.end(), comparator(n));