【发布时间】:2020-03-18 06:27:39
【问题描述】:
假设我有以下简单程序 (http://cpp.sh/5sygh):
#include <map>
#include <iostream>
using Key = std::pair<unsigned long, unsigned long long>;
struct KeyLess {
bool operator()(const Key& lhs, const Key& rhs) {
if (lhs.first < rhs.first) {
return true;
}
if (lhs.second < rhs.second) {
return true;
}
return false;
}
};
int main() {
std::map< Key , int, KeyLess> m;
m[Key{2, 169}] = 1;
m[Key{1, 255}] = 2;
m[Key{1, 391}] = 3;
m[Key{1, 475}] = 4;
std::cout << "Elements in map: " << m.size() << std::endl;
for(const auto &x: m) {
std::cout <<"Value: "<< x.second << std::endl;
}
}
输出仅包含 2 个项目,而不是地图中的 4 个:
Elements in map: 4
Value: 2
Value: 1
我错过了什么?
【问题讨论】:
标签: c++ dictionary c++11 stdmap ranged-loops