c++如何初始化多个字符串数组
时间: 2023-08-11 11:34:06 浏览: 171
你可以通过以下两种方式初始化多个字符串数组:
1. 逐个初始化
```cpp
#include <iostream>
#include <string>
int main() {
std::string str_arr1[3] = {"hello", "world", "!"};
std::string str_arr2[4] = {"how", "are", "you", "?"};
// 输出两个字符串数组的内容
for (int i = 0; i < 3; i++) {
std::cout << str_arr1[i] << " ";
}
std::cout << std::endl;
for (int i = 0; i < 4; i++) {
std::cout << str_arr2[i] << " ";
}
std::cout << std::endl;
return 0;
}
```
2. 使用二维数组初始化
```cpp
#include <iostream>
#include <string>
int main() {
std::string str_arr[2][3] = {{"hello", "world", "!"}, {"how", "are", "you"}};
// 输出两个字符串数组的内容
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 3; j++) {
std::cout << str_arr[i][j] << " ";
}
std::cout << std::endl;
}
return 0;
}
```
以上两种方式都可以初始化多个字符串数组,选择哪种方式取决于你的具体需求。
阅读全文