要使用C++的random_shuffle函數來創(chuàng)建測試數據,你可以按照以下步驟操作:
#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;
}
運行以上代碼,你將看到測試數據被隨機打亂后的結果。