C++快速入门(上)

必须了解的C++头文件

#define _CRT_SECURE_NO_WARNINGS//避免我们在编译老的用C语言的开源项目如lua源包的时候,可能因为一些老的.c文件使用了strcpy,scanf等不安全的函数,而报警告和错误,而导致无法编译通过。
#include  //标准输入输出函数 
#include  //数学函数库
#include  //字符串处理函数 
#include  //动态存储分配函数头文件
#include  //C++标准输入输出流头文件 
#include  //文件输出流
#include  //分配拓展的数组

条件编译

C++快速入门(上)_第1张图片
以#if开头以#endif结尾就构成一个条件编译
如果这个是0表示这里面的就跳过(不编译)

输入输出

std::cout << " 标准名字空间 " << std::endl;

Endl 相当于换行符

  • 也可在开头声明
using std::count;
using std::endl;
  • 还可以整体引入:
using namespace std;

输入:

  • cin >>

输出:

  • cout <<
  • Puts();
    文件输出与写入
#include  
#include  
#include  
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;
}

C++快速入门(上)_第2张图片

引用变量、引用形参

引用变量.

  • 引用变量是其他变量的别名。如同- -个人的外号或小名。
  • 既然是引用, 定义引用变量时就必须指明其引用的是哪个变量。
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  
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  
using namespace std;

void swap(int x, int y) {
	cout << x << '\t' << y << endl;
	int t = x;
	x = y; 
	y = t;
	cout << x << '\t' << y << endl;
}

int main() {
	int a = 3, b = 4;
	cout << a << '\t' << b << endl;
	swap(a, b);
	cout << a << '\t' << b  << endl;
}

C++快速入门(上)_第3张图片
这样传值,并不会发生改变。

void swap(int *x, int *y) {
	int t = *x;
	*x = *y;
	*y = t;
}


#include  
using namespace std;

int main() {
	int a = 3, b = 4;
	cout << a << '\t' << b << endl;
	swap(&a, &b);//以地址的方式传入
	cout << a << '\t' << b << endl;
}

在这里插入图片描述
除此之外,还可以这样:

//x和y作为引用变量
void swap(int &x, int &y) {
	int t = x;
	x = y;
	y = t;
}


#include  
using namespace std;

int main() {
	int a = 3, b = 4;
	cout << a << '\t' << b << endl;
	swap(a, b);
	cout << a << '\t' << b << endl;
}

在这里插入图片描述

函数的默认形参

函数的形参可以有默认值。

void print(char ch, int n =1);

●默认形参必须在非默认形参右边,即一律靠右

add(x=1,y, z=3); //false
add(y, x=1,z=3); //true

看两个例子:

#include  
using namespace std;

void print(char ch, int n = 1) {
	for (int i = 0; i < n; i++)
		cout << ch;
}

int main() {
	print('*'); cout << endl;//n没传值,就用默认值n=1
	print('*',3); cout << endl;//n有传值,就用已传的值3
	print('*',5); cout << endl;//n有传值,就用已传的值5
}

在这里插入图片描述

#include  
using namespace std;

int add(int x,int y=2,int z=3) {
	return x + y + z;
}


int main() {
	cout << add(5)<<endl;//传了一个值,赋值给了x
	cout << add(5,7) << endl;//传了两个值,赋值给了x,y
	cout << add(5,7,9) << endl;//传了三个值,赋值给了x,y,z
}

在这里插入图片描述

函数重载

●C+ +允许同意作用域里有同名的函数,只要它们的形参不同。如:

int add(int X, int y);
double add(double x, double y);

●函数名和形参列表构造了函数的签名。
●函数重载不能根据返回类型区分函数。如

int add(int X, int y); 
double add(int X, int y);
#include  
using namespace std;

int add(int x, int y = 2) {
	return x + y ;
}
double add(double x, double y = 2.0) {
	return x + y;
}

int main() {
	cout << add(5,3) << endl;
	cout << add(5.3, 7.8) << endl;	
	//这里不知道会调用int类型的add方法还是double类型的add方法所以会报错
	//cout << add(5, 7.8) << endl;
	cout << add((double)5, 7.8) << endl;//当然这里也可以为int类型
}

在这里插入图片描述

函数模板

  • 通用算法:函数模板。也称为泛型算法
int add(int x,int y) {
	return x + y;
}
double add (double x,double y ) {
	return x + y;
}
  • 用template关键字增加一个模板头,将数据类型变成类型模板参数。
template<typename T>
T add(Tx, Ty) {
	return x + y;
}
#include  
using namespace std;

int add(int x, int y) {
	return x + y;
}

double add(double x, double y ) {
	return x + y;
}

int main() {
	cout << add(5, 3) << endl;
	cout << add(5.3, 7.8) << endl;

}

在这里插入图片描述

模板参数自动推断

cout << add(53) << endl;
cout << add(5.37.8) << endl;
#include  
#include  
using namespace std;

template<typename T>
T add(T x, T y) {
	return x + y;
}

int main() {

	cout << add(5, 3) << endl;
	cout << add(5.3, 7.8) << endl;
	cout << add((double)5, 7.8) << endl; //同理,这里也要绑定类型,不然会有歧义性
  
}

在这里插入图片描述

#include  
#include  
using namespace std;

template<typename T>
T add(T x, T y) {
	return x + y;
}

int main() {

 cout << add<int>(5, 3) << endl;
 cout << add<double>(5.3, 7.8) << endl; 
 cout << add<int>(4, 6) << endl; 
 cout << add<string>("hello", "world") << endl; 
 
}

C++快速入门(上)_第4张图片

string

  • string是一个用户定义类型,表示的是符串。
string s = "hello", s2("world");
  • 用成员访问运算符访问string类的成员。
cout << s.size() << endl;
string s3 = s.substr(1,3);
cout << s3 << endl;
  • 用运算符对string对象进行运算,如+、[]
#include  
#include  
using namespace std;

int main() {
	string s = "hello", s2("world");
	//访问运算符.
  
	cout << s.size() << endl;
	string s3 = s.substr(1, 3);
	cout << s3<< endl;
	
	string s4 = s + " " + s2;
	cout << s4 << endl; //"hello world"
  
	
	s4[0] = 'H';
	s4[6] = 'V';
	cout << s4 << endl;//Hello Vorld
	
	int pos = s4.find("rld");//8
	cout << pos << endl;
	s4.insert(3, "AAAA");
	cout << s4 << endl;

	for (int i = 0; i < s4.size(); i++)
		cout << s4[i] << "-";
	cout << "\n";

}

C++快速入门(上)_第5张图片

vector

  • 向量,类似于数组,但可以动态增长。
    头文件< vector>
  • 是一个类模板,实例化产生一个类,如vector产生一个数据元素是int的vector< int>类(向量)
  • 同样,可以通过vector < int>类对象去访问其成员,如成员函数。
  • 同样可以用运算符进行一些运算。
#include  
#include  
using namespace std;

int main() {	
	vector<int> v = { 7, 5, 16, 8 };
	//push_back(),最后添加一个元素
  
	v.push_back(25);
	v.push_back(13);

	//成员函数size()、下标运算符[]
  
	for (int i = 0; i < v.size(); i++)
		cout << v[i] << '\t';
	cout << '\n';
	//pop_back(),去除最后一个元素
	v.pop_back();
	for (int i = 0; i < v.size(); i++)
		cout << v[i] << '\t';
	cout << '\n';
	
	v.resize(2);

	for (int i = 0; i < v.size(); i++)
		cout << v[i] << '\t';
	cout << '\n';
}

C++快速入门(上)_第6张图片

你可能感兴趣的:(C++,c++,开发语言,后端)