模板参数 deduction/substitution 失败:地图没有第一个,第二个吗?

template argument deduction/substitution failed: is there no first, second for map?

我希望使用 stl std::sort()std::map 进行排序,但在 geeksforgeekside 上出现错误(无法粘贴整个错误,请参阅 link)

#include <bits/stdc++.h>
using namespace std;

int main() {
    map<int,int> m{
        {1,11},
        {2,5},
        {3,0}
    };
    sort(begin(m),end(m),[](auto a, auto b){return a.second < b.second;});
    for(auto i: m)
        cout<<i.first<<" "<<i.second<<endl;
    return 0;
}

我试过了

sort(m.begin(),m.end(),[](pair<int,int> a, pair<int,int> b){returna.second < b.second;});

但问题仍然是没有 first, second for std::map??

这是一张地图,按设计键排序。

不能按值排序。

此外,不要使用 bits/stdc++.h

您不能使用 std::sort()std::map 进行排序。

如您所见,在 this page 中,std::sort()

的要求

Type requirements: RandomIt must meet the requirements of ValueSwappable and RandomAccessIterator.

std::map 的迭代器不是 RandomAccessIterator(类似于 std::vectorstd::deque 的迭代器)。

总之,std::map是一种自分类容器。

排序没有意义。有道理,如果你愿意,根据另一个比较器对其进行排序,你可以解释 std::map 的第三个模板参数(默认值:std::less,根据键)。