Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我刚开始用 C++ 编程,遇到了srand(time(0)). 谁能解释为什么有一个 0 in srand(time(0))?
srand(time(0))
因为这是 Ctime函数的签名:它需要一个指向存储时间的位置的指针,或者为 null。在这种情况下0,与文字空指针相同(这意味着不要将时间存储在备用位置)。
time
0
它使用当前时间值为随机数生成器播种,因此每次运行程序时它都会获得一个新值。
0 相当于 NULL。参数 totime()采用指向 a 的指针,time_t结果也可以存储在其中。
time()
time_t