要使用C++的random_shuffle函数来创建测试数据,你可以按照以下步骤操作:
#include <iostream>
#include <algorithm>
#include <vector>
#include <ctime>
std::vector<int> testData;
for (int i = 1; i <= 10; i++) {
testData.push_back(i);
}
std::cout << "Initial data: ";
for (int num : testData) {
std::cout << num << " ";
}
std::cout << std::endl;
std::srand(unsigned(std::time(0))); // 使用当前时间作为随机数种子
std::random_shuffle(testData.begin(), testData.end());
std::cout << "Shuffled data: ";
for (int num : testData) {
std::cout << num << " ";
}
std::cout << std::endl;
完整的C++代码如下所示:
#include <iostream>
#include <algorithm>
#include <vector>
#include <ctime>
int main() {
std::vector<int> testData;
for (int i = 1; i <= 10; i++) {
testData.push_back(i);
}
std::cout << "Initial data: ";
for (int num : testData) {
std::cout << num << " ";
}
std::cout << std::endl;
std::srand(unsigned(std::time(0))); // 使用当前时间作为随机数种子
std::random_shuffle(testData.begin(), testData.end());
std::cout << "Shuffled data: ";
for (int num : testData) {
std::cout << num << " ";
}
std::cout << std::endl;
return 0;
}
运行以上代码,你将看到测试数据被随机打乱后的结果。