Yes there is such a function, it is called std::accumulate
:
#include <algorithm>
#include <iostream>
#include <vector>
int main() {
std::vector<std::string> v { "a", "b" };
std::string r = std::accumulate( v.begin(), v.end(), std::string(""),[](std::string a,std::string b){ return a + " | " + b; });
std::cout << r;
}
output:
| a | b
Handling the boundaries correct (no |
in the beginning), the code gets a tiny bit more verbose: pass v.begin() + 1
and the inital string accordingly, but take care of the edge case of an empty v
.
However, not that this naive application of std::accumulate
is far from being efficient (see here for details).