初识c++(慢慢掌握,应用篇)。

1.c++入门基础

1.1C++发展历史

C++的起源可以追溯到1979年,当时BjarneStroustrup(本贾尼·斯特劳斯特卢普,这个翻译的名字不同的地方可能有差异)在贝尔实验室从事计算机科学和软件工程的研究工作。面对项目中复杂的软件开发任务,特别是模拟和操作系统的开发工作,他感受到了现有语言(如C语言)在表达能力、可维护性和可扩展性方面的不足。
1983年,Bjarne Stroustrup在C语言的基础上添加了⾯向对象编程的特性,设计出了C++语言的雏形,此时的C++已经有了类、封装、继承等核⼼概念,为后来的⾯向对象编程奠定了基础。⼀年该语言被正式命名为C++。在随后的⼏年中,C++在学术界和⼯业界的应用逐渐增多。⼀些⼤学和研究所开始将C++作为教学和研究的首选语言,⽽⼀些公司也开始在产品开发中尝试使用C++。这⼀时期,C++的标准库和模板等特性也得到了进⼀步的完善和发展。
C++的标准化⼯作于1989年开始,并成立了⼀个ANSI和ISO(International StandardsOrganization)国际标准化组织的联合标准化委员会。1994年标准化委员会提出了第⼀个标准化草
案。在该草案中,委员会在保持斯特劳斯特卢普最初定义的所有特征的同时,还增加了部分新特征。在完成C++标准化的第⼀个草案后不久,STL(Standard Template Library)是惠普实验室开发的⼀系列软件的统称。它是由Alexander Stepanov、Meng Lee和David R Musser在惠普实验室⼯作时所开发出来的。在通过了标准化第一个草案之后,联合标准化委员会投票并通过了将STL包含到C++标准中的提议。STL对C++的扩展超出C++的最初定义范围。虽然在标准中增加STL是个很重要的决定,但也因此延缓了C++标准化的进程。
1997年11月14日,联合标准化委员会通过了该标准的最终草案。1998年,C++的ANSI/IS0标准被投入使用。

2.c++参考文档

标准只更新到C++11,但好用
c++官方文档(中文)
c++官方文档(英文)

3.c++的第一个程序

C++兼容C语言绝⼤多数的语法,所以C语言实现的hello world依旧可以运行,C++中需要把定义⽂件代码后缀改为.cpp。

//test.cpp
#include<stdio.h>
int main()
{
	printf("hello world\n");
	return 0;
}

严格写法:

#include<iostream>
using namespace std;
int main()
{
	cout << "hello world\n" << endl;
	return 0;
}

运行结果

4. 命名空间

4.1 namespace的价值

使用命名空间的目的是对标识符的名称进行本地化,以避免命名冲突或名字污染。

4.2 namespace的定义

(1) 定义命名空间,需要使⽤到namespace关键字,后⾯跟命名空间的名字,然后接⼀对{}即可,{}中
即为命名空间的成员。命名空间中可以定义变量/函数/类型等。
(2)namespace本质是定义出⼀个域,这个域跟全局域各自独立,不同的域可以定义同名变量,所以下
面的rand不在冲突了。
(3) C++中域有函数局部域,全局域,命名空间域,类域;域影响的是编译时语法查找⼀个变量/函数/类型出处(声明或定义)的逻辑,所有有了域隔离,名字冲突就解决了。局部域和全局域除了会影响编译查找逻辑,还会影响变量的生命周期,命名空间域和类域不影响变量⽣命周期。

(4)namespace只能定义在全局,当然他还可以嵌套定义。
(5)项目工程中多文件中定义的同名namespace会认为是⼀个namespace,不会冲突。
(6) C++标准库都放在⼀个叫std(standard)的命名空间中。
1.正常的命名空间定义

#include<stdio.h>
#include <stdlib.h>
// spy是命名空间的名字,⼀般开发中是⽤项⽬名字做命名空间名。
namespace spy
{
	// 命名空间中可以定义变量/函数/类型
	int rand = 10;
	int Add(int left, int right)
	{
		return left + right;

	}
	struct Node {
		struct Node* next;
		int val;
	};

	
}
int main()
{

	// 这⾥默认是访问的是全局的rand函数指针
	printf("%p\n",rand);
	// 这⾥指定bit命名空间中的rand
	// ::域访问限定符
	printf("%d\n",spy::rand);
	return 0;
}

运行结果
2.命名空间可以嵌套

#include<stdio.h>
#include<stdlib.h>
namespace spy
{
	namespace sp
	{
		int rand = 10;
		int Add(int left, int right)
		{
			return left + right;

		}
		struct Node
		{
			struct Node* next;
			int val;
		};
	}
	namespace py
	{
		int rand = 1;
		int Add(int left, int right)
		{
			return (left + right)*10;

		}
		struct Node
		{
			struct Node* next;
			int val;
		};
	}
}
int main()
{
	printf("%d\n", spy::sp::rand);
	printf("%d\n", spy::sp::Add(1,2));
	printf("%d\n",spy::py::rand);
	printf("%d\n",spy::py::Add(1, 2));
	
	return 0;
}

运行结果
3.多文件中可以定义同名namespace,他们会默认合并到⼀起,就像同⼀个namespace⼀样
stack1.h

#pragma once
#include <stdio.h>
#include<stdlib.h>
#include<assert.h>
#include<stdbool.h>
namespace spy
{
	//定义栈的结构
	typedef int STDataType;
	typedef  struct Stack
	{
		STDataType* arr;
		int capacity;    //栈的空间大小
		int top;         //栈顶
	}ST;

	//初始化
	void STInit(ST* ps);
	//销毁
	void STDestroy(ST* ps);
	//栈顶  --入数据,出数据
	void StackPush(ST* ps, STDataType x);
	void StackPop(ST* ps);
	//判断栈是否为空
	bool StackEmpty(ST* ps);
	//取栈顶元素
	STDataType Stacktop(ST* ps);
	// 栈里的数据不能被遍历,也不能随机访问
	//获取栈中有效元素个数
	int STSize(ST* ps);
}

queue.h

#pragma once
#include<stdio.h>
#include<stdlib.h>
#include<assert.h>
#include<stdbool.h>
namespace  spy {
	//定义队列结构
	typedef int QDataType;
	typedef struct QueueNode
	{
		QDataType data;
		struct QueueNode* next;
	}QueueNode;
	typedef struct Queue
	{
		QueueNode* phead;
		QueueNode* ptail;
		int size;//队列有效个数
	}Queue;

	//初始化
	void QueueInit(Queue* pq);
	//入队列 ,队尾
	void QueuePash(Queue* pq, QDataType x);
	//队列判空
	bool QueueEmpty(Queue* pq);
	//出队列,对头
	void QueuePop(Queue* pq);
	//取队头数据
	QDataType QueueFront(Queue* pq);
	//取队尾数据
	QDataType QueueBack(Queue* pq);
	//队列有效元素个数
	int	QueueSize(Queue* pq);
	//销毁队列
	void QueueDestroy(Queue* pq);

}

stack.cpp

#define  _CRT_SECURE_NO_WARNINGS 1
#include"stack1.h"
namespace spy {
	//初始化
	void STInit(ST* ps)
	{
		assert(ps);
		ps->arr = NULL;
		ps->capacity = ps->top = 0;//栈顶等于栈底
	}

	//销毁
	void STDestroy(ST* ps) {
		assert(ps);
		if (ps->arr)

			free(ps->arr);
		ps->arr = NULL;
		ps->capacity = ps->top = 0;
	}
	//栈顶  --入数据,出数据
	void StackPush(ST* ps, STDataType x)//入栈
	{
		assert(ps);
		//判断空间是否足够
		if (ps->top == ps->capacity)//如果相等就说明栈满了  用栈顶和容量比大小
		{
			int newcapacity = ps->capacity == 0 ? 4 : 2 * ps->capacity;
			STDataType* tmp = (STDataType*)realloc(ps->arr, newcapacity * sizeof(STDataType));
			if (tmp == NULL)
			{
				perror("realloc fail!");
				exit(1);
			}
			ps->arr = tmp;//将tmp增容到arr
			ps->capacity = newcapacity;//增容完,容量增大;
		}
		//空间足够
		ps->arr[ps->top++] = x;//栈顶向上移动
	}
	//出栈
	void StackPop(ST* ps)
	{
		assert(ps);
		assert(!StackEmpty(ps));//如果不为空,top--
		--ps->top;

	}
	//判断栈是否为空
	bool StackEmpty(ST* ps)
	{
		assert(ps);
		return ps->top == 0;
	}
	//取栈顶元素
	STDataType  Stacktop(ST* ps)
	{
		assert(ps);
		assert(!StackEmpty(ps));
		return ps->arr[ps->top - 1];
	}
	//获取栈中有效元素个数
	int STSize(ST* ps)
	{
		assert(ps);
		return  ps->top;
	}
}

queue.cpp

#define  _CRT_SECURE_NO_WARNINGS 1
#include"queue.h"
namespace spy {
	//初始化
	void QueueInit(Queue* pq)
	{
		assert(pq);
		pq->phead = pq->ptail = NULL;
		pq->size = 0;
	}
	//入队列 ,队尾
	void QueuePash(Queue* pq, QDataType x)
	{
		assert(pq);
		QueueNode* newnode = (QueueNode*)malloc(sizeof(QueueNode));
		if (newnode == NULL)
		{
			/*	ferror("malloc fail!");*/
			exit(1);
		}
		newnode->data = x;
		newnode->next = NULL;
		//ptail newnode
		if (pq->phead == NULL)
		{
			//队列为空
			pq->phead = pq->ptail = newnode;

		}
		else
		{
			//队列不为空
			pq->ptail->next = newnode;
			pq->ptail = newnode;//pq->ptail->next
		}
		pq->size++;
	}
	//队列判空
	bool QueueEmpty(Queue* pq) {
		assert(pq);
		return pq->phead == NULL && pq->ptail == NULL;
	}

	//出队列,对头
	void QueuePop(Queue* pq) {
		assert(pq);
		assert(!QueueEmpty(pq));
		//只有一个结点的情况,避免ptail变成野指针
		if (pq->ptail == pq->phead)
		{
			free(pq->phead);
			pq->phead = pq->ptail = NULL;

		}
		else
		{
			//删除队头元素
			QueueNode* next = pq->phead->next;
			free(pq->phead);
			pq->phead = next;
		}
		pq->size--;
	}

	//取队头数据
	QDataType QueueFront(Queue* pq)
	{
		assert(pq);
		assert(!QueueEmpty(pq));//队列不为空
		return pq->phead->data;
	}
	//取队尾数据
	QDataType QueueBack(Queue* pq)
	{
		assert(pq);
		assert(!QueueEmpty(pq));//队列不为空
		return pq->ptail->data;
	}
	//队列有效元素个数
	int	QueueSize(Queue* pq)
	{
		assert(pq);
		//int size = 0;
		//QueueNode* pcur = pq->phead;
		//while (pcur)
		//{
		//	size++;
		//	pcur = pcur->next;
		//}
		//return size;
		return pq->size;
	}
	//销毁队列
	void QueueDestroy(Queue* pq)
	{
		assert(pq);
		assert(!QueueEmpty(pq));
		QueueNode* pcur = pq->phead;
		while (pcur)
		{
			QueueNode* next = pcur->next;
			free(pcur);
			pcur = next;
		}
		pq->phead = pq->ptail = NULL;
	}
}

test.cpp

#define  _CRT_SECURE_NO_WARNINGS 1
//多⽂件中可以定义同名namespace,他们会默认合并到⼀起,就像同⼀个namespace⼀样
#include"stack1.h"
#include"queue.h"
//定义全局的一份stack
typedef struct stack
{
	int a[10];
	int TOP;
}ST;
void STInit(ST* ps) {}
void STPush(ST* ps, int x) {}
int main()
{

	//调用全局的
	ST st1;
	STInit(&st1);
	STPush(&st1, 1);
	STPush(&st1, 2);
	printf("%d\n", sizeof(st1));
	//调用spy namespace的
	spy::ST st2;
	printf("%d\n", sizeof(st2));
	spy::STInit(&st2);
	spy::StackPush(&st2, 1);
	spy::StackPush(& st2, 2);

	return 0;
}

运行结果

4.3 命名空间使用

编译查找⼀个变量的声明/定义时,默认只会在局部或者全局查找,不会到命名空间里面去查找。所以
下⾯程序会编译报错。所以我们要使用命名空间中定义的变量/函数,有三种方式:
(1) 指定命名空间访问,项目中推荐这种方式。
(2)using将命名空间中某个成员展开,项目中经常访问的不存在冲突的成员推荐这种方式。
(3) 展开命名空间中全部成员,项目不推荐,冲突风险很⼤,日常小练习程序为了方便推荐使用。

  1. //指定命名空间访问
#include<stdio.h>
namespace  spy
{
	int a = 0;
	int b = 1;
}
int main()
{
	// 编译报错:error C2065: “a”: 未声明的标识符
	/*printf("%d", a);*/
	//指定命名空间访问
	printf("%d",spy::a);
	return 0;
}

2.// using将命名空间中某个成员展开

using spy:: b;
int main()
{
	printf("%d\n", spy::a);
	printf("%d\n", b);

	return 0;
}

3.// 展开命名空间中全部成员

using namespace spy;
int main()
{
	printf("%d\n", spy::a);
	printf("%d\n", spy::a);
	printf("%d\n", b);
	printf("%d\n",a);//展开命名空间中全部成员
	printf("%d\n", b);//展开命名空间中全部成员

	return 0;
}

运行结果

5. C++输⼊&输出

(1) 是 Input Output Stream 的缩写,是标准的输⼊、输出流库,定义了标准的输入、输
出对象。

(2) std::cin 是 istream 类的对象,它主要面向窄字符(narrow characters (of type char))的标准输
⼊流。

(3) std::cout 是 ostream 类的对象,它主要面向窄字符的标准输出流
(4) std::endl 是⼀个函数,流插⼊输出时,相当于插⼊⼀个换行字符加刷新缓冲区。
(5) <<是流插⼊运算符,>>是流提取运算符。(C语⾔还⽤这两个运算符做位运算左移/右移)
(6) 使用C++输入输出更方便,不需要像printf/scanf输⼊输出时那样,需要⼿动指定格式,C++的输入输出可以⾃动识别变量类型(本质是通过函数重载实现的,这个以后会讲到),其实最重要的是C++的流能更好的支持自定义类型对象的输⼊输出。
(7) IO流涉及类和对象,运算符重载、继承等很多面向对象的知识,这些知识我们还没有讲解,所以这
⾥我们只能简单认识⼀下C++ IO流的用法,后⾯我们会有专⻔的⼀个章节来细节IO流库。
(8) cout/cin/endl等都属于C++标准库,C++标准库都放在⼀个叫std(standard)的命名空间中,所以要通过命名空间的使用方式去用他们。
(9) ⼀般日常练习中我们可以using namespace std,实际项目开发中不建议using namespace std。
(10)这里我们没有包含<stdio.h>,也可以使⽤printf和scanf,在包含间接包含了。vs系列编译器是这样的,其他编译器可能会报错。

#include<iostream>

using namespace std;
int main()
{
	//在io需求比较高的地方,如部分大量输入的竞赛题中,加以下3行代码
	// 可以提高c++I0效率
	ios_base::sync_with_stdio(false);
	cin.tie(nullptr);
	cout.tie(nullptr);
	int a = 0;
	double b = 0.1;
	char c = 'x';
	std::cout << a << " " << b << " " << c << endl;
	scanf("%d%lf",&a,&b);
	printf("%d%lf",a,b);
	//可以自动识别变量的类型
	cin >> a;
	cin >> b >> c; 
	cout << a << endl;
	cout << b<<" "<< c << endl;

	return 0;
}

运行结果

6. 缺省参数

缺省参数是声明或定义函数时为函数的参数指定⼀个缺省值。在调用该函数时,如果没有指定实参则采用该形参的缺省值,否则使用指定的实参,缺省参数分为全缺省和半缺省参数。(有些地方把缺省参数也叫默认参数)
(1)全缺省就是全部形参给缺省值,半缺省就是部分形参给缺省值。C++规定半缺省参数必须从右往左依次连续缺省,不能间隔跳跃给缺省值。
(2)带缺省参数的函数调用,C++规定必须从左到右依次给实参,不能跳跃给实参。
(3) 函数声明和定义分离时,缺省参数不能在函数声明和定义中同时出现,规定必须函数声明给缺省值。

#include<iostream>
#include<assert.h>
using namespace std;
void fun(int a = 0)
{
	cout << a << endl;
}
int main()
{
	fun(); //没有传参时,使用参数的默认值
	fun(10);//传参时,使用指定参数
	return 0;
}

运行结果

#include<iostream>
using namespace std;
//全缺省
void Fun1(int a = 10, int b = 20, int c = 30)
{
	cout << "a = " << a << endl;
	cout << "b = " << b << endl;
	cout << "c = " << c << endl << endl;
}
//半缺省
void Fun2(int a, int b = 20, int c = 30)
{
	cout << "a = " << a << endl;
	cout << "b = " << b << endl;
	cout << "c = " << c << endl << endl;
}
int main()
{
	Fun1();
	Fun1(1);
	Fun1(1,2);
	Fun1(1,2,3);
	/*Fun1(,2,)*/;//不支持这种
	Fun2(100);
	Fun2(100, 200);
	Fun2(100, 200,300);
	return 0;
}

运行结果
stack1.h

#pragma once
//#include <stdio.h>
//#include<stdlib.h>
#include <iostream>
#include<assert.h>
//#include<stdbool.h>
using namespace std;
	//定义栈的结构
	typedef int STDataType;
	typedef  struct Stack
	{
		STDataType* arr;
		int capacity;    //栈的空间大小
		int top;         //栈顶
	}ST;

	//初始化
	void STInit(ST* ps, int n = 4);
	/*void  STInit(ST* ps);*/
	//销毁
	void STDestroy(ST* ps);
	//栈顶  --入数据,出数据
	void StackPush(ST* ps, STDataType x);
	void StackPop(ST* ps);
	//判断栈是否为空
	bool StackEmpty(ST* ps);
	//取栈顶元素
	STDataType Stacktop(ST* ps);
	// 栈里的数据不能被遍历,也不能随机访问
	//获取栈中有效元素个数
	int STSize(ST* ps);

stack.cpp

#define  _CRT_SECURE_NO_WARNINGS 1
#include"stack1.h"

	//初始化
	void STInit(ST* ps,int n)
	{
		assert(ps&&n>0);
		ps->arr = NULL;
		ps->arr = (STDataType*)malloc(n * sizeof(STDataType));
		ps->capacity = 0;//栈顶等于栈底
		ps->top = n;
	}
	

	//销毁
	void STDestroy(ST* ps) {
		assert(ps);
		if (ps->arr)

			free(ps->arr);
		ps->arr = NULL;
		ps->capacity = ps->top = 0;
	}
	//栈顶  --入数据,出数据
	void StackPush(ST* ps, STDataType x)//入栈
	{
		assert(ps);
		//判断空间是否足够
		if (ps->top == ps->capacity)//如果相等就说明栈满了  用栈顶和容量比大小
		{
			int newcapacity = ps->capacity == 0 ? 4 : 2 * ps->capacity;
			STDataType* tmp = (STDataType*)realloc(ps->arr, newcapacity * sizeof(STDataType));
			if (tmp == NULL)
			{
				perror("realloc fail!");
				exit(1);
			}
			ps->arr = tmp;//将tmp增容到arr
			ps->capacity = newcapacity;//增容完,容量增大;
		}
		//空间足够
		ps->arr[ps->top++] = x;//栈顶向上移动
	}
	//出栈
	void StackPop(ST* ps)
	{
		assert(ps);
		assert(!StackEmpty(ps));//如果不为空,top--
		--ps->top;

	}
	//判断栈是否为空
	bool StackEmpty(ST* ps)
	{
		assert(ps);
		return ps->top == 0;
	}
	//取栈顶元素
	STDataType  Stacktop(ST* ps)
	{
		assert(ps);
		assert(!StackEmpty(ps));
		return ps->arr[ps->top - 1];
	}
	//获取栈中有效元素个数
	int STSize(ST* ps)
	{
		assert(ps);
		return  ps->top;
	}

test.c

#define  _CRT_SECURE_NO_WARNINGS 1
//多⽂件中可以定义同名namespace,他们会默认合并到⼀起,就像同⼀个namespace⼀样
#include"stack1.h"

int main()
{
	ST s1;
	STInit(&s1);
	// 确定知道要插⼊1000个数据,初始化时⼀把开好,避免扩容
	ST s2;
	STInit(&s2, 1000);

	return 0;
}

7. 函数重载

**C++支持在同⼀作用域中出现同名函数,但是要求这些同名函数的形参不同,可以是参数个数不同或者类型不同。**这样C++函数调用就表现出了多态行为,使用更灵活。C语言是不支持同⼀作用域中出现同名函数的。

#include<iostream>
using namespace std;
//1.参数不同
int Add(int left, int right)
{
	cout << "int Add(int left,int right)" << endl;
	return left + right;
}
double Add(double left, double right)
{
	cout << "double Add(int left,int right)" << endl;
	return left + right;
}
//参数个数不同
void f()
{
	cout << "f() " << endl;
}
void f(int a)
{
	cout << "f(int a) " << endl;
}
//3.参数类型顺序不同 本质:类型不同
void f(int a,char b)
{
	cout << "f(int a,char b ) " << endl;
}
void f(char b,int a)
{
	cout << "f(char b,int a) " << endl;
}
//4.返回值不同不能作为重载条件,因为调用时也无法区分
//void ff();
//int ff();
//下面两个函数构成重载
//f()但是调用时,会报错,存在歧义,编译器不知道调用谁
void f1()
{
	cout << "f()" << endl;
}
void f1(int a = 10)
{
	cout << "f(int a)" << endl;
}
int main()
{
	Add(10, 20);
	Add(10.1, 20.2);
	f();
	f(10);
	f(10, 'a');
	f('a', 10);
	return 0;
}

运行结果

8. 引用

8.1 引用的概念和定义

引用不是新定义⼀个变量,⽽是给已存在变量取了⼀个别名,编译器不会为引用变量开辟内存空间,
它和它引用的变量共用同⼀块内存空间
。比如:水浒传中李逵,宋江叫"铁牛",江湖上⼈称"黑旋风";林冲,外号豹子头;
格式:类型& 引用别名 = 引用对象;

#include<iostream>
using namespace std;
int main()
{
	int a = 0;
	//引用:b和c是a的别名
	int& b = a;
	int& c = a;
	//也可以给别名b取别名,d相当于是a的别名
	int& d = b;
	++d;
	//这里取地址也是一样的
	cout << &a << endl;
	cout << &b << endl;
	cout << &c << endl;
	cout << &d << endl;
	return 0;
}

运行结果
上方a,b,c,d的取地址关系
关系

8.2 引用的特性

(1) 引用在定义时必须初始化
(2) ⼀个变量可以有多个引用
(3) 引用⼀旦引用⼀个实体,再不能引用其他实体(引用不能替代指针,引用不能改变指向。)

#include<iostream>
using namespace std;
int main()
{
	int a = 1;
	//编译报错:“ra”: 必须初始化引⽤
	/*int& ra;*/
	int& b = a;
	int c = 20;
	b = c;
	// 这⾥并⾮让b引⽤c,因为C++引⽤不能改变指向,
   // 这⾥是⼀个赋值
	cout << &a << endl;
	cout << &b << endl;
	cout << &c << endl;
	return 0;
}

在这里插入图片描述

8.3 引用的使用

(1)引用在实践中主要是于引用传参和引用做返回值中减少拷贝提高效率改变引用对象时同时改变被
引用对象。

(2) 引用传参跟指针传参功能是类似的,引用传参相对更方便⼀些。
(3) 引用返回值的场景相对比较复杂,我们在这里简单讲了⼀下场景。
(4)引用和指针在实践中相辅相成,功能有重叠性,但是各有特点,互相不可替代。C++的引用跟其他语言的引用(如Java)是有很⼤的区别的,除了用法,最大的点,C++引用定义后不能改变指向,Java的引用可以改变指向。

//交换
#include<iostream>
using namespace std;
void swap(int& c, int& d)
{
	int tmp = c;
	c = d;
	d = tmp;
}
int main()
{
	int a = 1, b = 2;
	cout << a <<" "<< b<< endl;
	swap(a,b);
	cout << a << " " << b << endl;
	return 0;
}

引用
引用

#define  _CRT_SECURE_NO_WARNINGS 1
#include<iostream>
#include<assert.h>
using namespace std;
typedef int STDataType;
typedef struct Stack
{
	STDataType* a;
	int top;
	int capacity;
}ST;
void STInit(ST& rs ,int n =4)
{
	rs.a = (STDataType*)malloc(n * sizeof(STDataType));
	rs.top = 0;
	rs.capacity = n;
}
//栈顶
void STPush(ST& rs, STDataType x)
{
	
	//满了,扩容
	if (rs.top == rs.capacity)
	{
		printf("扩容\n");
		int newcapacity = rs.capacity == 0 ? 4 : rs.capacity * 2;
		STDataType* tmp = (STDataType*)realloc(rs.a, newcapacity * sizeof(STDataType));
		if (tmp == NULL)
		{
			perror("realloc fail!");
			exit(1);
		}
		rs.a = tmp;
		rs.capacity = newcapacity;

	}
	rs.a[rs.top] = x;
	rs.top++;
}
//int STTop(ST& rs)
int &STTop(ST& rs)
{
	assert(rs.top > 0);
	return rs.a[rs.top-1];
}
int main()
{
	//调用全局的
	ST st1;
	STInit(st1);
	STPush(st1, 1);
	STPush(st1, 2);
	cout << STTop(st1) << endl;
	STTop(st1) += 10;
	cout << STTop(st1) << endl;
	return 0;
}

引用

#define  _CRT_SECURE_NO_WARNINGS 1
#include<iostream>
#include<assert.h>
using namespace std;
typedef struct SeqList
{
	int a[10];
	int size;
}SLT;
void SeqPushBack(SLT& sl, int x)
{

}
typedef struct ListNode
{
	int val;
	struct ListNode* next;

}LTNode,*PNode;
// 指针变量也可以取别名,这⾥LTNode*& phead就是给指针变量取别名
// 这样就不需要⽤⼆级指针了,相对⽽⾔简化了程序
//void ListPushBack(LTNode** phead, int x)
//void ListPushBack(LTNode*& phead, int x)
void ListPushBack(PNode&phead,int x)
{
	
	PNode newnode = (PNode)malloc(sizeof(LTNode));
	if (phead == NULL)
	{
		phead = newnode;
	}
	else
	{
		//找尾节点
		LTNode* pcur = phead;
		while (pcur->next)
		{
			pcur = pcur->next;

		}
		//pcur newnode 
		pcur->next = newnode;
	}
}
int main()
{
	PNode plist = NULL;
	ListPushBack(plist, 1);
	return 0;
}

9.const引用(引用对象:普通对象,const引用,临时对象)

(1)可以引用⼀个const对象,但是必须用const引用。const引用也可以引用普通对象,因为对象的访问权限在引⽤过程中可以缩小,但是不能放大(只能是指针或引用可以缩小但不能放大)
(2) 不需要注意的是类似 int& rb = a3; double d = 12.34; int& rd = d; 这样⼀些场景下a3的和结果保存在⼀个临时对象中, int& rd = d 也是类似,在类型转换中会产⽣临时对象存储中间值,也就是时,rb和rd引用的都是临时对象,而C++规定临时对象具有常性,所以这里就触发了权限放大,必须要用常引用才可以。
(3) 所谓临时对象就是编译器需要⼀个空间暂存表达式的求值结果时临时创建的⼀个未命名的对象,C++中把这个未命名对象叫做临时对象。
可以缩小,但是不能放大

#define  _CRT_SECURE_NO_WARNINGS 1
#include<iostream>
int main()
{
	const int a = 10;
	/*int& ra = a;*/
	// 编译报错:error C2440: “初始化”: ⽆法从“const int”转换为“int &”
    // 这⾥的引用是对a访问权限的放大
	const int& ra = a;//这样才行
	/*ra++;*/ // 编译报错:error C3892: “ra”: 不能给常量赋值
	
	// 这里的引用是对b访问权限的缩小
	int b = 20;
	const int& rb = b;
	/*rb++;*/// 编译报错:error C3892: “rb”: 不能给常量赋值


	return 0;
}

在这里插入图片描述

using namespace std;
int main()
{
	int a = 10;
	const int& sa = 30;
	/*int& sb = a * 3;*/  // 编译报错: “初始化”: ⽆法从“int”转换为“int &”
	const int& sb = a * 3;
	double c = 11.22;
	/*int& sc = c;*/ // 编译报错:“初始化”: ⽆法从“double”转换为“int &”
	const int& sc = c;
	return 0;
}

10.指针和引用的关系

C++中指针和引用就像两个性格迥异的亲兄弟,指针是哥哥,引用是弟弟,在实践中他们相辅相成,功能有重叠性,但是各有自己的特点,互相不可替代。
(1)语法概念上引用是⼀个变量的取别名不开空间,指针是存储⼀个变量地址,要开空间。(引用在汇编看来,引用也要用指针实现。)
(2)引用在定义时必须初始化,指针建议初始化,但是语法上不是必须的。
(3)引用在初始化时引用⼀个对象后,就不能再引用其他对象;而指针可以在不断地改变指向对象。
(4) 引用可以直接访问指向对象,指针需要解引用才是访问指向对象。
(5) sizeof中含义不同,引用结果为引用类型的大小,但指针始终是地址空间所占字节个数(32位平台下
占4个字节,64位下是8byte)
(6) 指针很容易出现空指针和野指针的问题,引用很少出现,引用使用起来相对更安全⼀些。

11. inline(替代宏函数)

(1) 用inline修饰的函数叫做内联函数,编译时C++编译器会在调用的地方展开内联函数,这样调用内联
函数就需要建立栈帧了,就可以提⾼效率。
(2)inline对于编译器而言只是⼀个建议,也就是说,你加了inline编译器也可以选择在调用的地方不展
开,不同编译器关于inline什么情况展开各不相同,因为C++标准没有规定这个。inline适用于频繁
调用的短小函数,对于递归函数,代码相对多⼀些的函数,加上inline也会被编译器忽略。
(3)C语言实现宏函数也会在预处理时替换展开,但是宏函数实现很复杂很容易出错的,且不方便调试,C++设计了inline目的就是替代C的宏函数。
(4) vs编译器 debug版本下 面默认是不展开inline的,这样方便调试,debug版本想展开需要设置⼀下
以下两个地方。
(5) inline不建议声明和定义分离到两个文件,分离会导致链接错误。因为inline被展开,就没有函数地址,链接时会出现报错。
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

#define  _CRT_SECURE_NO_WARNINGS 1
#include<iostream>
//int main()
//{
//	const int a = 10;
//	/*int& ra = a;*/
//	// 编译报错:error C2440: “初始化”: ⽆法从“const int”转换为“int &”
//    // 这⾥的引用是对a访问权限的放大
//	const int& ra = a;//这样才行
//	/*ra++;*/ // 编译报错:error C3892: “ra”: 不能给常量赋值
//	
//	// 这里的引用是对b访问权限的缩小
//	int b = 20;
//	const int& rb = b;
//	/*rb++;*/// 编译报错:error C3892: “rb”: 不能给常量赋值
//
//
//	return 0;
//}
//using namespace std;
//int main()
//{
//	int a = 10;
//	const int& sa = 30;
//	/*int& sb = a * 3;*/  // 编译报错: “初始化”: ⽆法从“int”转换为“int &”
//	const int& sb = a * 3;
//	double c = 11.22;
//	/*int& sc = c;*/ // 编译报错:“初始化”: ⽆法从“double”转换为“int &”
//	const int& sc = c;
//	return 0;
//}
using namespace std;
inline int Add(int x,int  y)
{
	int ret = x + y;
	ret += 1;
	ret += 1;
	ret += 1;
	return ret;
}
int main()
{
	// 可以通过汇编观察程序是否展开
// 有call Add语句就是没有展开,没有就是展开了
	int ret = Add(1, 2);
	cout << Add(1, 2) * 5 << endl;
	return 0;
}

#include<iostream>
using namespace std;
// 实现⼀个ADD宏函数的常见问题
//#define ADD(int a, int b) return a + b;
//#define ADD(a, b) a + b;
//#define ADD(a, b) (a + b)
// 正确的宏实现
#define ADD(a, b) ((a) + (b))
// 为什么不能加分号?  eg:cout << ADD(1, 2) << endl;
// 为什么要加外面的括号?  cout << ADD(1, 2)*5 << endl;
// 为什么要加里面的括号?   ADD(x & y, x | y);&|优先级低
int main()
{
	int ret = ADD(1, 2);
	cout << ADD(1, 2) << endl;
	cout << ADD(1, 2)*5 << endl;
	int a = 1, b = 2;
	ADD(a & b, a | b);// -> (x&y+x|y)
	return 0;
}

在这里插入图片描述
第五点

a.h
#include <iostream>
using namespace std;
inline void f(int i);
a.cpp
#define  _CRT_SECURE_NO_WARNINGS 1
#include "a.h"
void f(int i)
{
	cout << i << endl;
}
main.cpp
#define  _CRT_SECURE_NO_WARNINGS 1
#include "a.h"
int main()
{
	// 链接错误:⽆法解析的外部符号 "void __cdecl f(int)" (?f@@YAXH@Z)
	f(10);
	return 0;
}

在这里插入图片描述

12. nullptr

NULL实际是⼀个宏,在传统的C头文件(stddef.h)中,可以看到如下代码:

#ifndef NULL
#ifdef __cplusplus
#define NULL 0
#else
#define NULL ((void *)0)
#endif
#endif

(1)C++中NULL可能被定义为字⾯常量0,或者C中被定义为无类型指针(void*)的常量。不论采取何种定义,在使用空值的指针时,都不可避免的会遇到⼀些麻烦,本想通过f(NULL)调用指针版本的f(int*)函数,但是由于NULL被定义成0,调用了f(int x),因此与程序的初衷相悖。f((void*)NULL);调用会报错。
(2)C++11中引入nullptr,nullptr是⼀个特殊的关键字,nullptr是⼀种特殊类型的字面量,它可以转成任意其他类型的指针类型。使用nullptr定义空指针可以避免类型转换的问题,因为nullptr只能被隐式地转换为指针类型,⽽不能被转换为整数类型。

#include<iostream>
using namespace std;
void f(int x)
{
	cout << "f(int x)" << endl;
}
void f(int* ptr)
{
	cout << "f(int* ptr)" << endl;
}
int main()
{
	f(0);
	// 本想通过f(NULL)调⽤指针版本的f(int*)函数,但是由于NULL被定义成0,调⽤了f(intx),因此与程序的初衷相悖。
	f(NULL);
	f((int*)NULL);
	// 编译报错:error C2665: “f”: 2 个重载中没有⼀个可以转换所有参数类型
	 /*f((void*)NULL);*/
	f(nullptr);
     return 0;
}

在这里插入图片描述

总结

以上就是c++的一些入门知识点及应用,希望对大家有帮助,喜欢的可以一键三连。

  • 24
    点赞
  • 21
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

懒羊羊大王&

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值