在vc++中程序中用了srandom()和random(),头文件为stdlib.h,但编译出现错误error C3861: “srandom”: 找不到标识符。
原因是现在vc++编译器的库函数中没有randomize()和random(),分别用srand()和rand()代替了。
#include <time.h> //定义关于时间的函数
一般在用到time(NULL)(当前时间)函数时需要包含此头文件
#include <stdlib.h> //定义杂项函数及内存分配函数
一般在用到rand()和srand()函数时需要包含此头文件
函数名: random 功 能: 随机数发生器,也就是产生一个随机数
用 法: int random(int num);
产生的随机数范围为0~num-1。
函数名: randomize
功 能: 初始化随机数发生器,相当于拨随机种子
用 法: void randomize(void);
/************************************************************************/
/*
对于迭代器,有另一种方法使用流和标准函数。理解的要点是将输入/输出流作为容器看待。
因此,任何接受迭代器参数的算法都可以和流一起工作。
函数Display()显示了如何使用一个输出流迭代器。下面的语句将容器中的值传输到cout输出流对象中:
copy(v.begin(), v.end(),
ostream_iterator<int>(cout,"\t"));
第三个参数实例化了ostream_iterator<int>类型,并将它作为copy()函数的输出目标迭代器对象。“\t”字符串是作为分隔符。
*/
/************************************************************************/
#include <iostream>
#include <stdlib.h> // Need random(), srandom()
#include <time.h> // Need time()
#include <algorithm> // Need sort(), copy()
#include <vector> // Need vector
using namespace std;
void Display(vector<int>& v, constchar*s);
int main()
{
// Seed the random number generator
srand(time(NULL));
// Construct vector and fill with random integer values
vector<int> collection(10);
for (int i = 0; i <10; i++)
collection[i]= rand() % 10000;
// Display, sort, and redisplay
Display(collection, "Before sorting");
sort(collection.begin(), collection.end());
Display(collection, "After sorting");
return 0;
}
// Display labels and contents of integer vector v
void Display(vector<int>& v, constchar*s)
{
cout << endl<< s<< endl;
copy(v.begin(), v.end(),ostream_iterator<int>(cout, " "));
cout << endl;
}
联系客服