在vc++中程序中用了srandom()和random(),頭文件為stdlib.h,但編譯出現(xiàn)錯誤error C3861: “srandom”:
找不到標(biāo)識符。 /************************************************************************/ /* 對于迭代器,有另一種方法使用流和標(biāo)準(zhǔn)函數(shù)。理解的要點是將輸入/輸出流作為容器看待。 因此,任何接受迭代器參數(shù)的算法都可以和流一起工作。 函數(shù)Display()顯示了如何使用一個輸出流迭代器。下面的語句將容器中的值傳輸?shù)?/span>cout輸出流對象中: copy(v.begin(), v.end(), ostream_iterator<int>(cout,
"\t")); 第三個參數(shù)實例化了ostream_iterator<int>類型,并將它作為copy()函數(shù)的輸出目標(biāo)迭代器對象?!?/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, const
char*
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 label
s and contents of integer vector v void Display(vector<int>& v, const
char*
s) { cout << endl
<< s
<< endl; copy(v.begin(), v.end(),ostream_iterator<int>(cout, "
")); cout << endl; } |
|