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

标签 c++ type-conversion include header-files

<分区>

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

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

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

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

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

我收到错误的代码是:

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

最佳答案

解决此类问题的方法是为相关功能查阅合适的引用资料。一个备受推崇的 C++ 引用站点是 cppreference.com .在这种情况下,它是 reference for modf 开始于:

Defined in header <cmath>

这就是你的答案。

比较上述 C++ 版本(重载函数系列)在 C++ header <cmath> 中定义的引用资料与 reference for the C version在 C 头文件中定义 <math.h> :

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

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

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

关于c++ - 我应该使用哪个头文件而不是#include <bits/stdc++.h>,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46814855/

相关文章:

r - 将空白元素解释为 R 函数 Factor() 中的有效因子元素的好处?

c++ - QT 中的 INCLUDEPATH 不起作用

c - 为什么标准库在 .c 文件而不是头文件中有它们的#include <...>

c++ - Mongo C++ 客户端库没有看到我的 boost

c++ - 有条件地调用不同的构造函数

c++ - 在 .m 和 .mm 文件中调用 .cpp 函数,错误

Pandas 数据框将列类型转换为字符串或分类

c++ - 从 tcp 读取 float

ant - 在 Ant 构建任务中包含外部资源

c++ - C++ 中的 Malloc 错误?