一个最基础的程序必须要有一个重要的组成部分:头文件。以输出"Hello World!"的经典程序来说:
#include <iostream>
using namespace std;
int main() {
cout << "Hello World!" << endl;
return 0;
}
<iostream>就是头文件,要是没有它,这个程序就会报错。#include是导入头文件的指令,头文件就像一个大仓库,而#include就是打开仓库的钥匙。头文件有很多,例如:<iostream> <cmath> ……
万能头文件
<bits/stdc++.h>是一个万能头文件,俗称万能头。在使用万能头的情况下,使用任何函数都是可以编译成功的。如以下代码:
#include <bits/stdc++.h>
using namespace std;
int a , b , c , d[3] = {3 , 5 , 7};
int main() {
scanf("%d%d" &a , &b);
c = a + b;
cout << c << endl;
sleep(1);
swap(d[1] , d[2]);
cout << d[1] << endl;
sort(d + 0,d + 3);
return 0;
}
这个代码是可以编译成功的!!!!!!