为什么不能用const指定的元素类型对对进行排序?

时间:2019-09-24 14:18:08

标签: c++ sorting c++11 std-pair

我试图按值降序对Unique equations: 3*2=6 10/2+1=6 3*2/1=6 Unused numbers: {'4', '0', '5', '9', '7', '8'} 的元素进行排序。为此,我将键值对复制到std::map<char, int>中并尝试对其进行排序。然后,我收到了一堆墙的编译器错误文本(请参阅底部)。

显然std::vector<std::map<char, int>::value_type>解析为std::map<char, int>::value_type,看来std::pair<const char, int>引发的错误是由地图键类型的std::sort指定符引起的。因为当我改用const时效果很好。

这是代码:

std::pair<char, int>

为什么我不能直接使用#include <algorithm> #include <vector> int main() { using pair_type = std::pair<const char, int>; // fails to compile //using pair_type = std::pair<char, const int>; // fails to compile //using pair_type = std::pair<char, int>; // works std::vector<pair_type> to_be_sorted = {{'a', 1}, {'b', 2}, {'c', 3}}; std::sort(to_be_sorted.begin(), to_be_sorted.end(), [](pair_type const& a, pair_type const& b) { return a.second > b.second; } ); } ?当一对元素之一具有std::map<char, int>::value_type指定的类型时,为什么std::sort无法编译?

编译器错误如下:

const

0 个答案:

没有答案