c++中生成一个取字符串数组随机数的方法并在main函数中循环10次调用。
下面是一个生成随机字符串数组的方法,每次调用会生成一个随机的长度为10的字符串数组:
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
string generateRandomString() {
string str = "";
int len = 10;
for (int i = 0; i < len; i++) {
str += 'a' + rand() % 26;
}
return str;
}
int main() {
srand(time(NULL));
for (int i = 0; i < 10; i++) {
string arr[10];
for (int j = 0; j < 10; j++) {
arr[j] = generateRandomString();
cout << arr[j] << " ";
}
cout << endl;
}
return 0;
}
在main函数中,我们循环10次调用generateRandomString方法生成一个长度为10的字符串数组,并输出到控制台。每次运行结果都不同,因为每次生成的字符串是随机的
原文地址: https://www.cveoy.top/t/topic/eCDj 著作权归作者所有。请勿转载和采集!