我不确定要搜索什么。我找到了Renaming first and second of a map iterator,但这并不是我想做的事情。
下面是我想在下面看到的无意义的C++代码。有没有可能做到这一点?否则,我想只能选择“调整”迭代器作为循环中的第一行。
// what I want to do:
std::map<int, std::string> my_map;
// ... populate my_map
for(auto key, auto & value: my_map){
// do something with integer key and string value
}
C++11很好,但如果可能的话,最好避免使用boost。
我得到的最接近的是
// TODO, can this be templated?
struct KeyVal{
int & id;
std::string & info;
template <typename P>
KeyVal(P & p)
: id(p.first)
, info(p.second)
{
}
};
//...
for ( KeyVal kv : my_map ){
std::cout << kv.info;
}
但这意味着为每个map编写一个适配器类:(
// slightly joke answer/"what could possibly go wrong?"
#define key first
#define value second
https://stackoverflow.com/questions/36481173
复制相似问题