Why does rand() compile without including cstdlib or using namespace std?

前端 未结 3 1721
终归单人心
终归单人心 2020-12-11 03:49

According to the book I\'m reading, rand() requires #include in C++
However, I am able to compile the following code that uses

3条回答
  •  忘掉有多难
    2020-12-11 04:05

    iostream may include cstdlib directly or indirectly. This brings std::rand() and ::rand() in the scope. You are using the latter one.

    But yes, you should not count on this and always include cstdlib if you want to use rand. And in C++ code don't use rand, there are better ways to generate random numbers.

提交回复
热议问题