c++

c++怎么输入字符串数组

小亿
129
2024-02-19 17:18:30
栏目: 编程语言

在C++中,可以使用std::cin来输入字符串数组。下面是一个简单的示例代码,可以输入一个字符串数组并打印出来:

#include <iostream>
#include <string>

int main() {
    const int SIZE = 5;
    std::string arr[SIZE];

    // 输入字符串数组
    for (int i = 0; i < SIZE; i++) {
        std::cout << "Enter string " << i+1 << ": ";
        std::cin >> arr[i];
    }

    // 打印字符串数组
    std::cout << "You entered the following strings:" << std::endl;
    for (int i = 0; i < SIZE; i++) {
        std::cout << arr[i] << std::endl;
    }

    return 0;
}

在这个示例中,我们首先定义了一个大小为5的字符串数组arr,然后使用std::cin输入5个字符串,并将它们存储到数组中,最后打印出输入的字符串数组。

0
看了该问题的人还看了