我在 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() + ",";
}
}
但我不知道写什么好像条件