在vc++中程序中用了srandom()和random(),頭文件為stdlib.h,但編譯出現錯誤error C3861: “srandom”: 找不到標識符。
原因是現在vc++編譯器的庫函數中沒(méi)有randomize()和random(),分別用srand()和rand()代替了。
#include <time.h> //定義關(guān)于時(shí)間的函數
一般在用到time(NULL)(當前時(shí)間)函數時(shí)需要包含此頭文件
#include <stdlib.h> //定義雜項函數及內存分配函數
一般在用到rand()和srand()函數時(shí)需要包含此頭文件
函數名: random 功 能: 隨機數發(fā)生器,也就是產(chǎn)生一個(gè)隨機數
用 法: int random(int num);
產(chǎn)生的隨機數范圍為0~num-1。
函數名: randomize
功 能: 初始化隨機數發(fā)生器,相當于撥隨機種子
用 法: void randomize(void);
/************************************************************************/
/*
對于迭代器,有另一種方法使用流和標準函數。理解的要點(diǎn)是將輸入/輸出流作為容器看待。
因此,任何接受迭代器參數的算法都可以和流一起工作。
函數Display()顯示了如何使用一個(gè)輸出流迭代器。下面的語(yǔ)句將容器中的值傳輸到cout輸出流對象中:
copy(v.begin(), v.end(),
ostream_iterator<int>(cout,"\t"));
第三個(gè)參數實(shí)例化了ostream_iterator<int>類(lèi)型,并將它作為copy()函數的輸出目標迭代器對象?!?/span>\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;
}
聯(lián)系客服