Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 串联字符串_C++_String_Concatenation - Fatal编程技术网

C++ 串联字符串

C++ 串联字符串,c++,string,concatenation,C++,String,Concatenation,我想把一个向量合并成一个字符串,用空格分隔。比如说, sample string for this example 应成为本例的“示例字符串” 最简单的方法是什么 #include <iterator> #include <iostream> #include <sstream> #include <vector> #include <algorithm> std::vector<std::string> v; ...

我想把一个
向量
合并成一个
字符串
,用空格分隔。比如说,

sample
string
for
this
example
应成为本例的“示例字符串”
最简单的方法是什么
#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;
#包括 #包括 #包括 #包括 std::向量v; ... std::stringstream-ss; std::copy(v.begin(),v.end(),std::ostream_迭代器(ss,“”); std::string result=ss.str(); 如果(!result.empty()){ result.resize(result.length()-1);//修剪尾部空间 }
std::coutboost::join

没有好的例子,boost::join是一个痛苦的世界。本文档假设您对Boost Range库至少有一点熟悉。但需要修剪尾部空间,这让人感到遗憾。这似乎是Boost::join为您做的事情之一: