If you want user to enter the strings interactively, or if you want to read the strings from a file, then you can use the below method
#include <iostream>
#include <vector>
#include <numeric>
int main()
{
std::vector<std::string> v;
std::string s;
// Enter Ctrl-Z to terminate
while(std::cin >> s) {
v.push_back(s);
}
auto all = std::accumulate(v.begin(), v.end(), std::string(""));
std::cout << all << std::endl;
}
AAAA
DDDD
BBBB
CCCC
^Z
AAAADDDDBBBBCCCC
std::accumulate takes a binary predicate as its last argument. If you wish to add a separator between strings you can take advantage of this.
// define a delimiter
std::string delim{"-"};
// lambda to concatenate string with a separator
auto addDelimiter = [=](const std::string& s1, const std::string& s2) {
std::string result{s1};
if(!s1.empty() && !s2.empty())
result += delim;
result += s2;
return result;
};
auto all2 = std::accumulate(v.begin(), v.end(), std::string(""), addDelimiter);
std::cout << all2 << std::endl;
AAAA
BBBB
CCCC
DDDD
^Z
AAAA-BBBB-CCCC-DDDD