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:我沒有跟上。 –