使用 lambda 对 std::map 进行排序
Sorting a std::map using lambda
我有一个由键(字符串类型)和数据(元组类型)组成的映射。
我尝试使用 lambda 对我的地图进行排序(请参阅下面的代码)但是当我编译时出现错误
Severity Code Description Project File Line Suppression State
Error C2676 binary '-': 'const std::_Tree_unchecked_iterator>>>' does not define this operator or a conversion to a type acceptable to the predefined operator
with
[
_Kty=std::string,
_Ty=std::tuple
] SandBox C:\Program Files (x86)\Microsoft Visual Studio19\Community\VC\Tools\MSVC.23.28105\include\algorithm 3466
Severity Code Description Project File Line Suppression State
Error C2672 '_Sort_unchecked': no matching overloaded function found SandBox C:\Program Files (x86)\Microsoft Visual Studio19\Community\VC\Tools\MSVC.23.28105\include\algorithm 3466
我的代码:
#include <iostream>
#include <string>
#include <tuple>
#include <map>
#include <vector>
#include <algorithm>
#include <functional>
using namespace std;
int main() {
std::vector<std::string> strarr { "zone", "abigail", "theta", "form", "libe", "zas", "theta", "abigail"};
int length = strarr.size();
std::string str = "";
map<string, tuple<int, int>> myMap;
for (int i = 0; i < length; i++)
myMap[strarr[i]] = make_tuple(strarr[i].length(), ++(get<1>(myMap[strarr[i]])));
typedef std::function<bool(std::pair<string, tuple< int, int >>, std::pair<string, tuple< int, int >>)> Comparator;
Comparator compFunctor =
[](std::pair<string,tuple< int, int>> el1, std::pair<string, tuple< int, int >> el2)
{
return (get<0>(el1.second) < get<0>(el2.second));
};
std::sort(myMap.begin(), myMap.end(), compFunctor);
}
那么错误是什么?我确定这是愚蠢的事情,但我自己无法弄清楚。
提前致谢。
你的错误是 std::sort
需要 random access iterator
但 std::map 的迭代器是 bidirectional iterator
。您会看到 operator-()
没有为 bidrectional iterators
定义
查看类型要求 here。
除此之外,您所做的似乎很奇怪。看起来您正在尝试根据其值的第一个元素对地图进行排序。映射使用(默认情况下)std::less
键类型进行隐式排序。如果您想以不同的顺序排序,您应该在地图上使用自定义比较器。
std::map 是一棵 red-black 树。您不能按值对其进行排序。如果你想让你的地图按值排序,你需要翻转它。
struct compFunctor{
bool operator()(tuple< int, int> el1, tuple< int, int > el2) const {
return get<0>(el1) < get<0>(el2);
}
};
map< tuple<int, int>, string, compFunctor> myMap:
我有一个由键(字符串类型)和数据(元组类型)组成的映射。 我尝试使用 lambda 对我的地图进行排序(请参阅下面的代码)但是当我编译时出现错误
Severity Code Description Project File Line Suppression State Error C2676 binary '-': 'const std::_Tree_unchecked_iterator>>>' does not define this operator or a conversion to a type acceptable to the predefined operator with [ _Kty=std::string, _Ty=std::tuple ] SandBox C:\Program Files (x86)\Microsoft Visual Studio19\Community\VC\Tools\MSVC.23.28105\include\algorithm 3466
Severity Code Description Project File Line Suppression State Error C2672 '_Sort_unchecked': no matching overloaded function found SandBox C:\Program Files (x86)\Microsoft Visual Studio19\Community\VC\Tools\MSVC.23.28105\include\algorithm 3466
我的代码:
#include <iostream>
#include <string>
#include <tuple>
#include <map>
#include <vector>
#include <algorithm>
#include <functional>
using namespace std;
int main() {
std::vector<std::string> strarr { "zone", "abigail", "theta", "form", "libe", "zas", "theta", "abigail"};
int length = strarr.size();
std::string str = "";
map<string, tuple<int, int>> myMap;
for (int i = 0; i < length; i++)
myMap[strarr[i]] = make_tuple(strarr[i].length(), ++(get<1>(myMap[strarr[i]])));
typedef std::function<bool(std::pair<string, tuple< int, int >>, std::pair<string, tuple< int, int >>)> Comparator;
Comparator compFunctor =
[](std::pair<string,tuple< int, int>> el1, std::pair<string, tuple< int, int >> el2)
{
return (get<0>(el1.second) < get<0>(el2.second));
};
std::sort(myMap.begin(), myMap.end(), compFunctor);
}
那么错误是什么?我确定这是愚蠢的事情,但我自己无法弄清楚。
提前致谢。
你的错误是 std::sort
需要 random access iterator
但 std::map 的迭代器是 bidirectional iterator
。您会看到 operator-()
没有为 bidrectional iterators
查看类型要求 here。
除此之外,您所做的似乎很奇怪。看起来您正在尝试根据其值的第一个元素对地图进行排序。映射使用(默认情况下)std::less
键类型进行隐式排序。如果您想以不同的顺序排序,您应该在地图上使用自定义比较器。
std::map 是一棵 red-black 树。您不能按值对其进行排序。如果你想让你的地图按值排序,你需要翻转它。
struct compFunctor{
bool operator()(tuple< int, int> el1, tuple< int, int > el2) const {
return get<0>(el1) < get<0>(el2);
}
};
map< tuple<int, int>, string, compFunctor> myMap: