Something like this, if you can use boost
:
std::vector<std::string> s { "bob", "hey", "joe", "doe" };
std::vector<std::string> d;
for (auto i = std::begin(s); i != std::end(s); ++i) {
d.push_back(boost::algorithm::join(
boost::make_iterator_range(std::begin(s), i + 1),
std::string(" ")
));
}
The output vector d
will contain the following:
bob
bob hey
bob hey joe
bob hey joe doe
But more efficient solution is using temporary string:
std::vector<std::string> s { "bob", "hey", "joe", "doe" };
std::vector<std::string> d;
std::string t;
std::for_each(std::begin(s), std::end(s), [&](const std::string &i) {
d.push_back(t += (i + " "));
});