根据我正在阅读的书,rand() 在 C++ 中需要 #include
但是,我能够编译以下使用 rand() 的代码,而无需 #include <cstdlib> 或 using namespace std; 在 Visual 中工作室 2015.
为什么这两个不需要编译?我应该包括 cstdlib 吗?
According to the book I'm reading, rand() requires #include <cstdlib> in C++
However, I am able to compile the following code that uses rand() without #include <cstdlib> nor using namespace std; in Visual Studio 2015.
Why are these two not needed to compile? Should I include cstdlib?
C++ 代码:
#include <iostream>
int main()
{
std::cout << rand() << std::endl;
}
有两个问题在起作用:
iostream 可以直接或间接包含 cstdlib.cstdlib)的头文件可以将 C 标准库名称带入全局命名空间,即在 std 命名空间之外(例如 rand.)这是从 C++11 开始正式允许的,并且在之前被广泛容忍.iostream may include cstdlib directly or indirectly.cstdlib) are allowed to bring C standard library names into the global namespace, that is, outside of the std namespace (e.g. rand.) This is formally allowed since C++11, and was largely tolerated before.这篇关于为什么 rand() 编译时不包含 cstdlib 或使用命名空间 std?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!
读取输入文件,最快的方法?read input files, fastest way possible?(读取输入文件,最快的方法?)
在 C++ 中读取格式化输入的最简单方法?The easiest way to read formatted input in C++?(在 C++ 中读取格式化输入的最简单方法?)
从 .txt 文件读取到 C++ 中的二维数组Reading from .txt file into two dimensional array in c++(从 .txt 文件读取到 C++ 中的二维数组)
如何在 C++ 中模拟按键按下How to simulate a key press in C++(如何在 C++ 中模拟按键按下)
为什么在 cin.ignore() 之后没有 getline(cin, var) 读取Why doesn#39;t getline(cin, var) after cin.ignore() read the first character of the string?(为什么在 cin.ignore() 之后没有 getline(cin, var) 读取
scanf 格式输入的 cin 类比是什么?What is the cin analougus of scanf formatted input?(scanf 格式输入的 cin 类比是什么?)