To concatenate strings in an array in C++, you can use functions like `std::accumulate` or a loop to iterate through the array and concatenate each element.
In this example,
#include <iostream>
#include <vector>
#include <algorithm>
#include <numeric>
#include <string>
int main() {
std::vector<std::string> words = {"Hello", "World", "!"};
std::string concatenated = std::accumulate(words.begin(), words.end(), std::string(),
[](const std::string& a, const std::string& b) {
return a + " " + b;
});
std::cout << "Concatenated String: " << concatenated << std::endl;
return 0;
}
Concatenated String: Hello World !
In this example,
#include <iostream>
#include <vector>
#include <string>
int main() {
std::vector<std::string> words = {"Hello", "World", "!"};
std::string concatenated;
for (const std::string& word : words) {
concatenated += word + " ";
}
std::cout << "Concatenated String: " << concatenated << std::endl;
return 0;
}
Concatenated String: Hello World !
In this tutorial, we learned How to Concatenate Strings in Array in C++ language with well detailed examples.