Concatenating strings

為{幸葍}努か 提交于 2020-01-11 03:15:33

问题


I want to join a vector<string> into a single string, separated by spaces. For example,

sample
string
for
this
example

should become "sample string for this example".
What is the simplest way to do this?


回答1:


#include <iterator>
#include <iostream>
#include <sstream>
#include <vector>
#include <algorithm>

std::vector<std::string> v;
...

std::stringstream ss;
std::copy(v.begin(), v.end(), std::ostream_iterator<std::string>(ss, " "));
std::string result = ss.str();
if (!result.empty()) {
    result.resize(result.length() - 1); // trim trailing space
}
std::cout << result << std::endl;



回答2:


boost::join



来源:https://stackoverflow.com/questions/720344/concatenating-strings

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!