我应该使用哪个 header 文件而不是 #include <bits/stdc++.h>

Which header file should I use instead of #include <bits/stdc++.h>

#include <iostream>
#include <string>
#include <sstream>
//#include <bits/stdc++.h>
#include <iomanip>      // std::setprecision
#include <math.h> 
using namespace std;

我想删除 header #include <bits/stdc++.h>,因为它会显着减慢我的编译时间。

当我删除它时,出现以下错误:

error: cannot convert ‘long double*’ to ‘double*’ for argument ‘2’ to ‘double modf(double, double*)’
       fractpart = modf(val, &intpart);

我认为问题是缺少 header 文件,但不知道是哪一个文件。

我收到错误的代码是:

fractpart = modf(val, &intpart);
if (fractpart != 0) {
    throw Error("ERR");
}

解决此类问题的方法是查阅相关函数的合适参考资料。一个备受推崇的 C++ 参考站点是 cppreference.com. In this case, its reference for modf,开头为:

Defined in header <cmath>

这就是你的答案。

将 C++ 头文件 <cmath> 中定义的 C++ 版本(重载函数系列)的上述参考与 C 头文件 <math.h> 中定义的 reference for the C version 进行比较:

float modff( float arg, float* iptr );
double modf( double arg, double* iptr );
long double modfl( long double arg, long double* iptr );

C 没有函数重载,所以 <math.h> 中的 modf 只是 double 版本。 <cmath>,作为 C++,声明了所有 3 个 C++ 重载(floatdoublelong double),您正在使用其中的最后一个。

这实际上是远离 C 标准库头文件 (<*.h>) 并使用 C++ 标准库头文件 (<c*>) 的原因之一。