C++ 使用倒序打印带逗号的列表
C++ Printing lists with commas using reverse order
我在 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() + ",";
}
}
但是不知道写什么好像条件
我终于用上了这个解决方案
std::map<const std::string, JsonNode *>::reverse_iterator it;
for (it = vals.rbegin(); it != vals.rend(); it++){
str += it->first + ":" +it->second->toString() + ",";
}
str.erase(str.size() - 1);
谢谢。
如果将索引迭代器与 --vals.rend()
进行比较,它应该可以工作。请参阅下面的示例。
#include <map>
#include <string>
#include <iostream>
using MyMap = std::map<const std::string, std::string>;
std::string f(MyMap const &vals)
{
std::string str;
MyMap::const_reverse_iterator it;
MyMap::const_reverse_iterator first = --vals.rend();
for (it = vals.rbegin(); it != vals.rend(); it++) {
if (it == first) {
str += it->first + ":" + it->second;
} else {
str += it->first + ":" + it->second + ",";
}
}
return str;
}
int main()
{
MyMap a{{"a", "b"}, {"c","d"}};
std::cout << f(a) << "\n";
}
第一个示例中不需要 reverse_iterator
。你也应该使用 stringstream
因为添加字符串是非常低效的。以下是我的做法:
ostringstream stream(str);
auto it = vals.begin();
auto last = --vals.end();
for (; it != vals.end(); it++) {
if (it == last) {
stream << it->first << ":" << it->second->toString();
} else {
stream << it->first << ":" << it->second->toString() << ",";
}
}
str = stream.str();
我假设您至少使用 C++11,如果您没有,则只需将 auto
替换为实际类型即可。
关于倒序,你可以在上面的例子中使用reverse_iterator
而不是iterator
,它也会起作用。
如果你有一个ostream_joiner
迭代器(要么来自this answer or std::experimental
),它就变得非常简单
std::stringstream ss;
auto elem_to_string = [](MyMap::const_reference pair){ return pair.first + ":" + pair.second; };
std::transform(vals.rbegin(), vals.rend(), make_ostream_joiner(ss, ","), elem_to_string);
return ss.str();
与非反转版本类似。
我在 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() + ",";
}
}
但是不知道写什么好像条件
我终于用上了这个解决方案
std::map<const std::string, JsonNode *>::reverse_iterator it;
for (it = vals.rbegin(); it != vals.rend(); it++){
str += it->first + ":" +it->second->toString() + ",";
}
str.erase(str.size() - 1);
谢谢。
如果将索引迭代器与 --vals.rend()
进行比较,它应该可以工作。请参阅下面的示例。
#include <map>
#include <string>
#include <iostream>
using MyMap = std::map<const std::string, std::string>;
std::string f(MyMap const &vals)
{
std::string str;
MyMap::const_reverse_iterator it;
MyMap::const_reverse_iterator first = --vals.rend();
for (it = vals.rbegin(); it != vals.rend(); it++) {
if (it == first) {
str += it->first + ":" + it->second;
} else {
str += it->first + ":" + it->second + ",";
}
}
return str;
}
int main()
{
MyMap a{{"a", "b"}, {"c","d"}};
std::cout << f(a) << "\n";
}
第一个示例中不需要 reverse_iterator
。你也应该使用 stringstream
因为添加字符串是非常低效的。以下是我的做法:
ostringstream stream(str);
auto it = vals.begin();
auto last = --vals.end();
for (; it != vals.end(); it++) {
if (it == last) {
stream << it->first << ":" << it->second->toString();
} else {
stream << it->first << ":" << it->second->toString() << ",";
}
}
str = stream.str();
我假设您至少使用 C++11,如果您没有,则只需将 auto
替换为实际类型即可。
关于倒序,你可以在上面的例子中使用reverse_iterator
而不是iterator
,它也会起作用。
如果你有一个ostream_joiner
迭代器(要么来自this answer or std::experimental
),它就变得非常简单
std::stringstream ss;
auto elem_to_string = [](MyMap::const_reference pair){ return pair.first + ":" + pair.second; };
std::transform(vals.rbegin(), vals.rend(), make_ostream_joiner(ss, ","), elem_to_string);
return ss.str();
与非反转版本类似。