Hi,
I was trying to write a function to concat all the elements in a vector<string> into single string. I was trying to use the stl for_each algorithm and using its return type as the concatenated string. I was told that for_each has the same return type as the function provided to it, so why does this not compile ? and how do I get it to work ?
Code:
#include<iostream>
#include<vector>
#include<algorithm>
#include<string>
using namespace std;
string concat(string& s);
int main()
{
vector<string> u(10, "Hello");
string v;
v.reserve(1024);
v = for_each(u.begin(),u.end(),concat);
cout << v << endl;
return 0;
}
string concat(string& s)
{
static string str;
str += s;
return str;
}
Thank you!