在vc++中程序中用了srandom()和random(),头文件为stdlib.h,但编译出现错误error C3861: “srandom”:
找不到标识符。 /************************************************************************/ /* 对于迭代器,有另一种方法使用流和标准函数。理解的要点是将输入/输出流作为容器看待。 因此,任何接受迭代器参数的算法都可以和流一起工作。 函数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, 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; } |
|