c++关于random_shuffle的问题。
#include<iostream>#include<cstdlib>//#include<iomanip>//#include<string>//#include<cs...
#include<iostream>
#include<cstdlib>
//#include<iomanip>
//#include<string>
//#include<cstring>
//#include<cstdarg>
//#include<ctype.h>
//#include<conio.h>
#include<vector>
//#include<deque>
//#include<list>
//#include<set>
//#include<map>
#include<algorithm>
using namespace std;
//typedef
typedef int I;
typedef char C;
//宏定义
#define N 3
#define X(a,b) (((a)+(b))*3)
//自定义函数声明
void f1();
void f2();
void output(I);
//主函数
int main(int argc,char * argv[])
{
f1();
cout<<endl;
f2();
cout<<endl;
cout<<argc<<ends<<* argv<<endl;
system("pause");
return 0;
}
//自定义函数
void f1()
{
//定义vector对象
vector<I> intVector;
for(I i=0;i<10;i++)
{
//push_back()
intVector.push_back(i);
}
cout<<"Vect:";
//for_each
for_each(intVector.begin(),intVector.end(),output);
//random_shuffle
random_shuffle(intVector.begin(),intVector.end());
cout<<endl;
cout<<"Vect:";
//for_each()
for_each(intVector.begin(),intVector.end(),output);
}
void output(I x)
{
cout<<x<<endl;
}
void f2()
{
I arr[5]={1,2,3,4,5};
cout<<"arr:";
for(I i=0;i<5;i++)
{
cout<<arr[i]<<ends;
}
cout<<endl;
//random_shuffle
random_shuffle(& arr[0],& arr[5]);
cout<<"arr:";
for(I i=0;i<5;i++)
{
cout<<arr[i]<<ends;
}
cout<<endl;
}
为什么每次的随机结果都一样? 展开
#include<cstdlib>
//#include<iomanip>
//#include<string>
//#include<cstring>
//#include<cstdarg>
//#include<ctype.h>
//#include<conio.h>
#include<vector>
//#include<deque>
//#include<list>
//#include<set>
//#include<map>
#include<algorithm>
using namespace std;
//typedef
typedef int I;
typedef char C;
//宏定义
#define N 3
#define X(a,b) (((a)+(b))*3)
//自定义函数声明
void f1();
void f2();
void output(I);
//主函数
int main(int argc,char * argv[])
{
f1();
cout<<endl;
f2();
cout<<endl;
cout<<argc<<ends<<* argv<<endl;
system("pause");
return 0;
}
//自定义函数
void f1()
{
//定义vector对象
vector<I> intVector;
for(I i=0;i<10;i++)
{
//push_back()
intVector.push_back(i);
}
cout<<"Vect:";
//for_each
for_each(intVector.begin(),intVector.end(),output);
//random_shuffle
random_shuffle(intVector.begin(),intVector.end());
cout<<endl;
cout<<"Vect:";
//for_each()
for_each(intVector.begin(),intVector.end(),output);
}
void output(I x)
{
cout<<x<<endl;
}
void f2()
{
I arr[5]={1,2,3,4,5};
cout<<"arr:";
for(I i=0;i<5;i++)
{
cout<<arr[i]<<ends;
}
cout<<endl;
//random_shuffle
random_shuffle(& arr[0],& arr[5]);
cout<<"arr:";
for(I i=0;i<5;i++)
{
cout<<arr[i]<<ends;
}
cout<<endl;
}
为什么每次的随机结果都一样? 展开
1个回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询