我试图按值降序对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