accumulate算法可以将string类的容器中的元素相加吗
如果可以该怎么编写呢
vector<string>v;
v.push_back("a");
v.push_back("b");
v.push_back("c");
cout<<accumulate(v.begin(),v.end(),"hello");
accumulate算法可以将string类的容器中的元素相加吗
如果可以该怎么编写呢
vector<string>v;
v.push_back("a");
v.push_back("b");
v.push_back("c");
cout<<accumulate(v.begin(),v.end(),"hello");
#include <iostream>
#include <vector>
#include <numeric>
using namespace std;
int main() {
vector<string> words{"this ", "is ", "a ", "sentence!"};
string init, res;
res = accumulate(words.begin(), words.end(), init); // 连接字符串
cout << res << endl; // this is a sentence!
return 0;
}