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

   2024-11-04 8060
核心提示:在C++中,可以使用std::cin来输入字符串数组。下面是一个简单的示例代码,可以输入一个字符串数组并打印出来:#include iostream

在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个字符串,并将它们存储到数组中,最后打印出输入的字符串数组。

 
举报打赏
 
更多>同类维修大全
推荐图文
推荐维修大全
点击排行

网站首页  |  关于我们  |  联系方式网站留言    |  赣ICP备2021007278号