Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/sorting/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sorting 如何按第二个元素对向量v[]的数组排序_Sorting_Vector - Fatal编程技术网

Sorting 如何按第二个元素对向量v[]的数组排序

Sorting 如何按第二个元素对向量v[]的数组排序,sorting,vector,Sorting,Vector,我有一对向量数组{V[x].push_-back(make_-pair(y,w));} 我想用第二个元素对它进行排序。。在C++中,它是如何做到的?< p>你需要把比较函数作为第三个参数传递给排序函数。就像你提到的,向量的形式是: {V[x].push_back(make_pair(y,w));} 然后比较函数将如下所示: bool greaterThan( pair<int,int> lx, pair<int,int> rx ){ return lx.sec

我有一对向量数组{V[x].push_-back(make_-pair(y,w));}
我想用第二个元素对它进行排序。。在C++中,它是如何做到的?

< p>你需要把比较函数作为第三个参数传递给排序函数。就像你提到的,向量的形式是:

 {V[x].push_back(make_pair(y,w));}
然后比较函数将如下所示:

bool greaterThan( pair<int,int> lx, pair<int,int> rx ){
    return lx.second < rx.second; //change here for different criterias
}

//used as
sort(V[x].begin(), V[x].end(), greaterThan);
bool greaterThan(对lx,对rx){
返回lx.second
std::sort
传递比较函数:请参见(3)您是在询问如何对数组排序,还是如何比较向量?很抱歉,我不明白。编辑:使用lambda exp.:
std::sort(V[x].begin(),V[x].end(),[](std::pair a,std::pair b){返回a.second
实际上,您需要在不带括号的情况下通过
greaterThan
。您是对的@nngmg。如果使用C++提供大于< /Calp>比较器函数,那么只需要那些括号。谢谢。@VishalTheBeast实际上我有一个带v[x]的无向图。向后推(使成对(y,w))和v[y]。向后推(使成对(x,w)),我想按w排序。应排序(V[x].begin(),V[x].end(),大于);工作还是我应该在V中通过y?