【发布时间】:2018-12-20 13:16:24
【问题描述】:
我在 C++ 中有以下代码
std::map<const std::string, JsonNode *>::iterator it;
std::map<const std::string, JsonNode *>::reverse_iterator last = vals.rbegin();
last++;
for (it = vals.begin(); it != vals.end(); it++){
if (it == last.base())
{
str += it->first + ":" +it->second->toString();
}else{
str += it->first + ":" +it->second->toString() + ",";
}
}
它工作得很好,但需要以相反的顺序来做同样的事情。 我就是这样开始的
std::map<const std::string, JsonNode *>::iterator first = vals.begin();
std::map<const std::string, JsonNode *>::reverse_iterator it;
first++;
for (it = vals.rbegin(); it != vals.rend(); it++){
if (<condition>)
{
str += it->first + ":" +it->second->toString();
}else{
str += it->first + ":" +it->second->toString() + ",";
}
}
但我不知道写什么好像条件
【问题讨论】:
-
base()仅适用于std::map<...>::reverse_iterator。我正在寻找std::map<...>::iterator上的等价物
标签: c++ dictionary iterator reverse-iterator