必须了解的C++头文件
#define _CRT_SECURE_NO_WARNINGS//避免我们在编译老的用C语言的开源项目如lua源包的时候,可能因为一些老的.c文件使用了strcpy,scanf等不安全的函数,而报警告和错误,而导致无法编译通过。
#include <cstdio> //标准输入输出函数
#include <cmath> //数学函数库
#include <cstring> //字符串处理函数
#include <malloc.h> //动态存储分配函数头文件
#include <iostream> //C++标准输入输出流头文件
#include <fstream> //文件输出流
#include <vector> //分配拓展的数组
条件编译
以#if开头以#endif结尾就构成一个条件编译
如果这个是0表示这里面的就跳过(不编译)
输入输出
std::cout << " 标准名字空间 " << std::endl;
Endl 相当于换行符
- 也可在开头声明
using std::count;
using std::endl;
- 还可以整体引入:
using namespace std;
输入:
- cin >>
输出:
- cout <<
- Puts();
文件输出与写入
#include <fstream>
#include <iostream>
#include <string>
using namespace std;
int main() {
ofstream oF("test.txt");
oF << 666 << " " << "hello C++\n";
oF.close();
ifstream iF("test.txt");
double d;
string str;
iF >> d >> str;
cout<<d <<" "<< str<<endl;
return 0;
}
引用变量、引用形参
引用变量.
- 引用变量是其他变量的别名。如同- -个人的外号或小名。
- 既然是引用, 定义引用变量时就必须指明其引用的是哪个变量。
int a =3;
int &r=a;
- 引用变量“从一而终”,一旦定义就不能再弓|用其他变量
int &er=a;
int&r=b;
- 引用变量和被弓|用的变量类型必须匹配。
double d;
int&r=d;
- 对弓|用变量的操作就是对它引|用的变量的操作。
int a =3, &r=a;
cout<<a<< '\t' < <endl;
r=5;
cout<<a<< '\t' < <endl;
函数的值形参
C函数的形参 都是值参数,形参作为函数的局部变量有自 己单独的内存块,当函数调用时,实参将值拷贝(赋值给)形参。对形参的修改不会影响实参。
#include <iostream>
using namespace std;
int main() {
int a = 3, &t = a;
cout << a << '\t' << t << endl;
t = 5;
cout << a << '\t' << t << endl;
return 0;
}
此时a 与 t 为同一值。
#include <iostream>