以下是一个示例代码,生成一个长度为10的字符串数组,每次循环随机取其中一个字符串并输出。

#include <iostream>
#include <string>
#include <cstdlib>
#include <ctime>

using namespace std;

int main() {
    // 生成字符串数组
    string strArr[10] = {"apple", "banana", "orange", "watermelon", "kiwi",
                        "mango", "grape", "pineapple", "peach", "pear"};
    // 初始化随机数生成器
    srand(time(0));
    // 循环10次
    for (int i = 0; i < 10; i++) {
        // 随机生成数组下标
        int index = rand() % 10;
        // 取出对应的字符串并输出
        cout << strArr[index] << endl;
    }
    return 0;
}
``
c++中生成一个字符串数组并循环10次调用每次要取随机数。

原文地址: https://www.cveoy.top/t/topic/eCAO 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录