Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/133.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

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
C++ 如何对向量进行排序<;配对<;int,pair<;int,pair<;字符串,成对<;int,int>&燃气轮机&燃气轮机&燃气轮机>;?_C++_Sorting_Vector_Stl - Fatal编程技术网

C++ 如何对向量进行排序<;配对<;int,pair<;int,pair<;字符串,成对<;int,int>&燃气轮机&燃气轮机&燃气轮机>;?

C++ 如何对向量进行排序<;配对<;int,pair<;int,pair<;字符串,成对<;int,int>&燃气轮机&燃气轮机&燃气轮机>;?,c++,sorting,vector,stl,C++,Sorting,Vector,Stl,我正在学习使用STL的排序函数,将其用于一些复杂的向量对 我有以下向量: vector< pair< int , pair< int , pair< string , pair< int , int > > > > > vector

我正在学习使用STL的排序函数,将其用于一些复杂的向量对

我有以下向量:

vector< pair< int , pair< int , pair< string , pair< int , int > > > > >
vector>
我需要首先根据元素对中的第一个整数对元素进行排序,如果有两个元素具有相同的值,那么我需要根据内部元素对中的整数对它们进行排序

如果我将上述类型表示为:

vector< pair< I , pair< G , pair< S , pair< T , T > > > > >
向量>

首先,我需要根据I,然后根据G对它们进行排序。仅使用比较器是否可以有效地进行排序?

调用传递适当的比较函数作为
comp
。元素将按您希望的顺序进行比较。

对于您的特定情况,将使用
std::pair
中的默认比较

模板
布尔运算符>>>MyComplexType;
std::向量v;
//填充v
//分类
auto complexLessThan=[](常量MyComplexType&left,常量MyComplexType&right)->bool
{
//你的分类标准在这里
}
排序(v.begin(),v.end(),complexLessThan);

您的排序代码是什么样子的?你有什么代码可以分享吗?我已经厌倦了在其他答案中重复这些。词典比较。您所需要做的就是调用
std::sort
,它将立即生效。看看标准库的威力吧。我需要一个结构比较器来完成这项工作。我没有C++11。到目前为止,你做了哪些尝试。编写自定义比较运算符是一个非常常见的问题,我认为默认的比较运算符就可以了。几个模板化的词典比较运算符。问题:“我想使用
std::sort
,但如何编写比较器?”回答:“使用合适的比较器调用
std::sort
”。问题是如何对向量排序,而不是如何编写比较器。“仅使用比较器就可以有效地实现这一点吗?”是的。@Joker\u vD,答案是标准库将生成适当的
operator@StoryTeller的确这很好,因为我见过导致“有趣”错误的非总比较器。
template< class T1, class T2 >
bool operator<( const pair<T1,T2>& lhs, const pair<T1,T2>& rhs );
typedef pair< int , pair< int , pair< string , pair< int , int > > > > MyComplexType;
std::vector<MyComplexType> v;
// fill v

// sort
auto complexLessThan = [](const MyComplexType& left, const MyComplexType& right)  -> bool
{
    // your sorting criterion here           
}

std::sort(v.begin(), v.end(), complexLessThan);