将一组字符串转换为一个简单的字符串 C++
Convert a set of strings into a simple string c++
我有一组字符串set<string> aSet
。如何将集合转换为一个字符串,所有元素都用逗号分隔?谢谢!
这是一种选择:
std::ostringstream stream;
std::copy(aSet.begin(), aSet.end(), std::ostream_iterator<std::string>(stream, ","));
std::string result = stream.str();
accumulate example 具有将整数向量连接到字符串的代码,可以根据您的目的轻松转换:
std::string s = std::accumulate( std::begin(aSet),
std::end(aSet),
std::string{},
[](const std::string& a, const std::string &b ) {
return a.empty() ? b
: a + ',' + b; } );
这里是简单易读的方式,没有任何花哨的地方:
string s;
for (auto const& e : aSet)
{
s += e;
s += ',';
}
s.pop_back();
我有一组字符串set<string> aSet
。如何将集合转换为一个字符串,所有元素都用逗号分隔?谢谢!
这是一种选择:
std::ostringstream stream;
std::copy(aSet.begin(), aSet.end(), std::ostream_iterator<std::string>(stream, ","));
std::string result = stream.str();
accumulate example 具有将整数向量连接到字符串的代码,可以根据您的目的轻松转换:
std::string s = std::accumulate( std::begin(aSet),
std::end(aSet),
std::string{},
[](const std::string& a, const std::string &b ) {
return a.empty() ? b
: a + ',' + b; } );
这里是简单易读的方式,没有任何花哨的地方:
string s;
for (auto const& e : aSet)
{
s += e;
s += ',';
}
s.pop_back();