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?
#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;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With