随机获取0~9 不重复的四个数字
#include<iostream>
#include<string>
#include<algorithm>
#include<ctime>
using namespace std;
string Random_Result() {
char temp[] = "1234567890";
random_shuffle(temp, temp + 10);
//random_shuffle 用于打乱字符数组
//temp指向第0个位置
//temp+10 指向字符串的结束符'0'
temp[4] = 0; //把字符数组的第五个位置用数字0替换,因为整数0 是字符串结束标志,或者改为'\0'也可以
return temp; //返回temp前四个字符
}
int main() {
srand(unsigned(time(0)));
string res_String = Random_Result();
cout << res_String << endl;
}

浙公网安备 33010602011771号