📜  通过示例在 C++ 中使用“stdafx.h”标头

📅  最后修改于: 2022-05-13 01:55:07.487000             🧑  作者: Mango

通过示例在 C++ 中使用“stdafx.h”标头

头文件包含一组预定义的标准库函数。可以使用 C 预处理指令“#include”将头文件包含在程序中。所有头文件都有“ .h”扩展名。

句法:

#include 告诉编译器在执行程序语句之前将 header_file 带入源代码。

“stadax.h”头文件

这个头文件是一个预编译的头文件。这个头文件的主要目的是减少编译时间。此头文件通常用于 Microsoft Visual Studio。

没有 stadax.h 头文件:

在下面的程序中,使用了两个头文件 iostream 和 cmath。每次编译程序时,每次编译程序时,每个头文件都将从头开始编译。因此,每次编译的编译时间都是相同的。

C++
// C++ program to implement
// the above approach
#include 
#include 
using namespace std;
  
// Driver code
int main()
{
    cout << "GFG!";
    return 0;
}


C++
// C++ program to implement
// the above approach
#include "stdafx.h"
#include 
#include 
using namespace std;
  
// Driver code
int main()
{
    cout << "GFG!";
    return 0;
}


使用 stadax.h 头文件:

当我们想要一次又一次地编译程序时,“stadax.h”会派上用场。它必须包含在所有头文件的顶部,现在第一次编译程序时,程序中存在的所有头文件的编译版本将保存在“stadax.h”文件中。现在,每次编译程序时,编译器都会从“stadax.h”文件中挑选头文件的编译版本,而不是一次又一次地从头开始编译相同的头文件。

例子:

在该程序中,包含“stadax.h”头文件。现在,一旦程序编译完成,cmath 和 iostream 将从头开始编译,并且 cmath 和 iostream 的编译版本将保存在“stadafx.h”文件中。下次编译时,编译器会自动从“stadafx”头文件中选择这些头文件的编译版本。

C++

// C++ program to implement
// the above approach
#include "stdafx.h"
#include 
#include 
using namespace std;
  
// Driver code
int main()
{
    cout << "GFG!";
    return 0;
}

stadax.h 头文件的优点:

  • 它减少了那些需要一次又一次编译的程序的编译时间。
  • 它减少了不必要的处理。