I\'d like to build a std::string
from a std::vector
.
I could use std::stringsteam
, but imagine there is a s
If requires no trailing spaces, use accumulate
defined in
with custom join lambda.
#include
#include
#include
using namespace std;
int main() {
vector v;
string s;
v.push_back(string("fee"));
v.push_back(string("fi"));
v.push_back(string("foe"));
v.push_back(string("fum"));
s = accumulate(begin(v), end(v), string(),
[](string lhs, const string &rhs) { return lhs.empty() ? rhs : lhs + ' ' + rhs; }
);
cout << s << endl;
return 0;
}
Output:
fee fi foe fum