c++中,为什么头文件包含了stdlib.h,但是random函数显示有错。
2012-04-12 16:07
393 查看
#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 srandom( time(NULL) ); // Construct vector and fill with random integer values vector<int> collection(10); for (int i = 0; i < 10; i++) collection[i] = random() % 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, const char* s) { cout << endl << s << endl; copy(v.begin(), v.end(),ostream_iterator<int>(cout, "\t")); cout << endl; }
运行:
3.cpp
E:\workspace\test\3.cpp(14) : error C2065: 'srandom' : undeclared identifier
E:\workspace\test\3.cpp(19) : error C2065: 'random' : undeclared identifier
执行 cl.exe 时出错.
2.exe - 1 error(s), 0 warning(s)
原来:在linux下stdlib.h包含srandom 和random ,但在VC下stdlib.h包含的是srand和rand,所以应该改过来
#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) ); // srandom----->srand // Construct vector and fill with random integer values vector<int> collection(10); for (int i = 0; i < 10; i++) collection[i] = rand() % 10000; // random----->rand // 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, const char* s) { cout << endl << s << endl; copy(v.begin(), v.end(),ostream_iterator<int>(cout, "\t")); cout << endl; }
相关文章推荐
- atof函数在不包含头文件stdlib.h的情况下也能编译运行,但是转换结果是错误的,为什么
- C++中,头文件stdlib.h主要包含的函数
- C++中,头文件stdlib.h主要包含什么函数? C++
- c++中头文件stdlib.h主要包含的函数
- C++中inline函数的定义为什么要放在头文件中?头文件中“只定义一份”是什么意思?const object 和inline函数是“一次定义规则”
- C编程笔录(一)1.0: 在C编程中,函数的声明为什么都习惯的写在头文件中,然后在需要调用的地方使用#include来包含?
- c/c++常用头文件包含函数(转)
- 学习笔记之C++为什么将函数声明或者类的定义放在.h文件中,而将其实现放在原文件中
- C++:为什么inline函数的定义要放在头文件里 + inline和宏定义的区别(暂未体会到)
- C++头文件包含内容概览及字符函数库中常用的函数
- 【c++】为什么类的定义中不能包含其自身类型,但是能包含其自身的指针或引用类型
- 为何HttpURLConnection上传文件进度显示传完了,但是却一直卡在那不动了
- 【C++】基类析构函数为什么要为虚函数
- 为什么C++静态成员函数不能访问非静态成员变量、不能调用非静态成员函数?(面试常考)
- C++中父类的析构函数为什么要为虚函数
- asp下实现格式化文件大小以MB显示的函数
- PHP包含文件函数include、include_once、require、require_once区别总结
- C++ 头文件格式和函数注释格式
- c++头文件包含 #ifndef ##pragma once
- 怎样向一个c++函数传数组、Linux下怎么显示unicode字符、Linux的地址空间