0
我有下面的代码片段:C++流和运营商>>优先
std::stringstream stream("ABC 123 CBA 321");
std::unordered_map<std::string, int> map;
std::string key;
而且我想读这样的数据:
stream>>key>>map[key];
可以将它改写为:
operator>>(stream, key).operator>>(map[key]);
考虑到>>
和.
运营商有从左到右的联想性,你能否认罪se解释为什么map[key]
在从流中读取key
之前进行评估?
打印地图:
for(auto& it : map)
{
std::cout<<it.first<<" "<<it.second<<std::endl;
}
结果:
123
ABC 321
是的,有一个空间之前123
也许在C++ 17的严格评估顺序上添加注释也会很好。 –
@ PatrickM'Bongo:我没有跟上。 –