To declare a string array in C++, you can use array initialization syntax or the std::array or std::vector classes to create an array with string elements.
In this example,
stringArray
using array initialization syntax with predefined string values.#include <iostream>
#include <array>
int main() {
std::array<std::string, 3> stringArray = {"Hello", "World", "C++"};
for (const auto& str : stringArray) {
std::cout << str << "\n";
}
return 0;
}
Hello World C++
In this example,
emptyStringArray
using std::vector class without any initial elements.#include <iostream>
#include <vector>
int main() {
std::vector<std::string> emptyStringArray;
for (const auto& str : emptyStringArray) {
std::cout << str << "\n";
}
return 0;
}
In this tutorial, we learned How to Declare a String Array in C++ language with well detailed examples.