I'd like to build a std::string
from a std::vector<std::string>
.
I could use std::stringsteam
, but imagine there is a shorter way:
std::string string_from_vector(const std::vector<std::string> &pieces) {
std::stringstream ss;
for(std::vector<std::string>::const_iterator itr = pieces.begin();
itr != pieces.end();
++itr) {
ss << *itr;
}
return ss.str();
}
How else might I do this?
You could use the
std::accumulate()
standard function from the<numeric>
header (it works because an overload ofoperator +
is defined forstring
s which returns the concatenation of its two arguments):Alternatively, you could use a more efficient, small
for
cycle: