I want to be able to sort the following vector - vector< pair< string , pair< int ,int > > > based on the 1st element of the pair< int , int> and if those are equal then sort them according to their second elements, how do I do that in C++ using STL's constructs?
后我希望能够排序向量,向量 <对<字符串,对< int,int> > >基于第一元素的< int,int >,如果这些是相等的然后他们根据他们的第二个元素排序,我该怎么做在c++中使用STL的构造吗?
This sort has to accomplish something on the lines of this
这种类型必须在这方面有所作为
lets say E1 and E2 are 2 elements
假设E1和E2是两个元素。
if E1.second.first == E2.second.first then the comparison must be made with respect to the second elements.
如果E1.second。第一次= = E2.second。首先,必须对第二要素进行比较。
3 个解决方案
#1
2
If you can't use C++11 features you can still do something like this:
如果你不能使用c++ 11的特性,你仍然可以这样做:
typedef std::pair<std::string, std::pair<int, int>> AnkitSablok;
struct my_compare {
bool operator()(const AnkitSablok &lhs, const AnkitSablok &rhs) const {
return lhs.second < rhs.second;
}
};
int main()
{
std::vector<AnkitSablok> vec;
std::sort(vec.begin(), vec.end(), my_compare());
}
#2
2
[...] based on the 1st element of the pair< int , int> and if those are equal then sort them according to their second elements [...]
[…]基于这一对的第一个元素< int, int>,如果它们是相等的,那么根据它们的第二个元素对它们进行排序[…]
std::pair
already has lexicographical comparison C++03 20.2.2/6:
:对已经有字典比较的C+ 03 20.2.2/6:
template <class T1, class T2>
bool operator<(const pair<T1, T2>& x, const pair<T1, T2>& y);
Returns: x.first < y.first || (!(y.first < x.first) && x.second < y.second)
So, as WhozCraig pointed out, you should just compare .second
s of outer pair.
所以,正如WhozCraig所指出的,你应该比较外部对的。秒。
This is a lambda expression, I dont have C++ 11 with me, is there no other way possible?
这是一个lambda表达式,我没有带c++ 11,难道没有其他方法吗?
Use functor:
使用函子:
struct LessSecond
{
template<typename T, typename U>
bool operator()(const std::pair<T,U> &x, const std::pair<T,U> &y) const
{
return x.second < y.second;
}
};
// ...
sort(x.begin(), x.end(), LessSecond());
Or maybe more generic version (depends on your needs):
或者更通用的版本(取决于你的需要):
struct LessSecondGeneric
{
template<typename Pair>
bool operator()(const Pair &x, const Pair &y) const
{
return x.second < y.second;
}
};
现场演示:
#include <algorithm>
#include <iostream>
#include <iterator>
#include <utility>
#include <vector>
struct LessSecond
{
template<typename T, typename U>
bool operator()(const std::pair<T,U> &x, const std::pair<T,U> &y) const
{
return x.second < y.second;
}
};
int main()
{
using namespace std;
vector<pair<string , pair<int, int>>> x
{
{"1", {2, 1}}, {"2", {1, 1}}, {"3", {1, 2}}
};
sort(x.begin(), x.end(), LessSecond());
for(const auto &p : x)
cout << p.first << " (" << p.second.first << ", " << p.second.second << ")" << endl;
}
Output is:
输出是:
2 (1, 1)
3 (1, 2)
1 (2, 1)
#3
0
sort(x.begin, x.end, [](const X & a, const X & b){return a.second.first < b.second.first ; }) ;
Where x is your container, and X is the type of an element.
其中x是容器,x是元素的类型。
#1
2
If you can't use C++11 features you can still do something like this:
如果你不能使用c++ 11的特性,你仍然可以这样做:
typedef std::pair<std::string, std::pair<int, int>> AnkitSablok;
struct my_compare {
bool operator()(const AnkitSablok &lhs, const AnkitSablok &rhs) const {
return lhs.second < rhs.second;
}
};
int main()
{
std::vector<AnkitSablok> vec;
std::sort(vec.begin(), vec.end(), my_compare());
}
#2
2
[...] based on the 1st element of the pair< int , int> and if those are equal then sort them according to their second elements [...]
[…]基于这一对的第一个元素< int, int>,如果它们是相等的,那么根据它们的第二个元素对它们进行排序[…]
std::pair
already has lexicographical comparison C++03 20.2.2/6:
:对已经有字典比较的C+ 03 20.2.2/6:
template <class T1, class T2>
bool operator<(const pair<T1, T2>& x, const pair<T1, T2>& y);
Returns: x.first < y.first || (!(y.first < x.first) && x.second < y.second)
So, as WhozCraig pointed out, you should just compare .second
s of outer pair.
所以,正如WhozCraig所指出的,你应该比较外部对的。秒。
This is a lambda expression, I dont have C++ 11 with me, is there no other way possible?
这是一个lambda表达式,我没有带c++ 11,难道没有其他方法吗?
Use functor:
使用函子:
struct LessSecond
{
template<typename T, typename U>
bool operator()(const std::pair<T,U> &x, const std::pair<T,U> &y) const
{
return x.second < y.second;
}
};
// ...
sort(x.begin(), x.end(), LessSecond());
Or maybe more generic version (depends on your needs):
或者更通用的版本(取决于你的需要):
struct LessSecondGeneric
{
template<typename Pair>
bool operator()(const Pair &x, const Pair &y) const
{
return x.second < y.second;
}
};
现场演示:
#include <algorithm>
#include <iostream>
#include <iterator>
#include <utility>
#include <vector>
struct LessSecond
{
template<typename T, typename U>
bool operator()(const std::pair<T,U> &x, const std::pair<T,U> &y) const
{
return x.second < y.second;
}
};
int main()
{
using namespace std;
vector<pair<string , pair<int, int>>> x
{
{"1", {2, 1}}, {"2", {1, 1}}, {"3", {1, 2}}
};
sort(x.begin(), x.end(), LessSecond());
for(const auto &p : x)
cout << p.first << " (" << p.second.first << ", " << p.second.second << ")" << endl;
}
Output is:
输出是:
2 (1, 1)
3 (1, 2)
1 (2, 1)
#3
0
sort(x.begin, x.end, [](const X & a, const X & b){return a.second.first < b.second.first ; }) ;
Where x is your container, and X is the type of an element.
其中x是容器,x是元素的类型。