0
点赞
收藏
分享

微信扫一扫

每天五分钟计算机视觉:人脸识别网络FaceNet

敬亭阁主 2024-09-01 阅读 27

​​​​​​​



目录

C++的第⼀个程序

命名空间

namespace的价值

namespace的定义

命名空间的使用

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

 C++输⼊&输出

std::cout标准输出流

std::endl换行

 std::cin的标准输⼊流

缺省参数

 全缺省

半缺省

 函数重载

参数类型不同

参数个数不同

参数的顺序不同

返回函数不同不是函数重载

引⽤

引⽤的概念和定义

引⽤的特性

 引⽤的使⽤

const引⽤

指针和引⽤的关系

inline展开

nullptr


C++的第⼀个程序

C++兼容C语⾔绝⼤多数的语法,所以C语⾔实现的helloworld依旧可以运⾏,C++中需要把定义⽂件 代码后缀改为.cpp,vs编译器看到是.cpp就会调⽤C++编译器编译,linux下要⽤g++编译,不再是gcc

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

当然C++有⼀套⾃⼰的输⼊输出,严格说C++版本的helloworld应该是这样写的。

// test.cpp
// 这⾥的std cout等我们都看不懂,没关系,下⾯我们会依次讲解 
#include<iostream>
using namespace std;

int main()
{
 cout << "hello world\n" << endl;
 
 return 0;
}

命名空间

namespace的价值

在C/C++中,变量、函数和后⾯要学到的类都是⼤量存在的,这些变量、函数和类的名称将都存在于全 局作⽤域中,可能会导致很多冲突。使⽤命名空间的⽬的是对标识符的名称进⾏本地化,以避免命名 冲突或名字污染,namespace关键字的出现就是针对这种问题的。 c语⾔项⽬类似下⾯程序这样的命名冲突是普遍存在的问题,C++引⼊namespace就是为了更好的解决 这样的问题。

#include <stdio.h> 


#include <stdlib.h>
int rand = 10;

int main()
{
	// 编译报错:error C2365: “rand”: 重定义;以前的定义是“函数” 
	printf("%d\n", rand);
	return 0;
}

namespace的定义

下面这代码,命名空间为bit,在使用bit命名空间的时候,前面要加bit::

局部域出了生命周期就销毁了。
命名空间域就是为了跟全局域进行隔离的,不能把命名空间域定义在局部。

int x = 0;//全局域

namespace bit
{
	int x = 1;//命名空间域
}

void func()
{
	//这个局部域只能在当前局部域内访问
	int x = 2;//局部域
}

int main()
{
	int x = 3;//局部域

	printf("%d\n", x);//这个会在局部搜索,再到全局搜索
	printf("%d\n", bit::x);//访问命名空间域
	printf("%d\n", ::x);//访问全局

	return 0;
}

命名空间可以嵌套

//命名空间可以嵌套
namespace bit
{
	//在bit命名空间嵌套a和b的命名空间
	namespace a
	{
		int tab = 99;
		int add(int x, int y)
		{
			return x + y;
		}
	}

	namespace b
	{
		int tab = 10;
		int att(int x, int y)
		{
			return x * y;
		}
	}
}

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

	printf("%d \n", bit::a::add(13, 23));
	printf("%d \n", bit::b::att(5, 5));
}

多⽂件中可以定义同名namespace,他们会默认合并到⼀起,就像同⼀个namespace⼀样


命名空间的使用

编译查找⼀个变量的声明/定义时,默认只会在局部或者全局查找,不会到命名空间⾥⾯去查找。所以下⾯程序会编译报错。所以我们要使⽤命名空间中定义的变量/函数,有三种⽅式:


下面这个代码我们可以看到“a”未声明的标识符,因为a不能访问到fang里的a。

#include<iostream>

namespace fang
{
	int a = 10;
	int b = 20;
}

int main()
{
	 编译报错:error C2065: “a”: 未声明的标识符
	printf("%d \n", a);
	return 0;
}
// 指定命名空间访问
int main()
{
printf("%d\n", fang::a);
return 0;
}

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

using可以把命名空间的成员暴露到全局,。

注意的是全局变量的名字不能和命名空间的成员名字一样。

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

using namespace fang;//默认到局部找,再到全局找

int main()
{
	printf("%d\n", a);//fang暴露到全局后,就不用加fang::了
	printf("%d\n", b);
	return 0;
}

展开命名空间中全部成员,项⽬不推荐,冲突⻛险很⼤,⽇常⼩练习程序为了⽅便推荐使⽤。

下面这代码,using单独暴露a在全局变量,

打印b可以访问fang里的b成员,也可以访问全局变量的b,单独暴露可以避免全部暴露带来的名字冲突。

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

namespace fang
{
	int a = 10;
	int b = 20;
}

using fang::a;//把a单独暴露到全局

int b = 99;

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

	printf("%d\n", b);//全局的b
	return 0;
}

结果:


 C++输⼊&输出

std::cout标准输出流,它是把数值转换成字符输出到屏幕(终端)上的,如果本身就是字符就不用转换

std::cin的标准输⼊流,它就是把在屏幕(终端)上输入的字符转换成对应的整行浮点型,给给变量。

std::endl是⼀个函数,流插⼊输出时,相当于插⼊⼀个换⾏字符加刷新缓冲区。


std::cout标准输出流

c++的标准输出流(可以自动识别类型)然后输出,不像c语言一样,需要指定类型输出。

int main()
{
	// << 流插入
	std::cout << "您好";
	int a = 10;
	std::cout << a;

	double b = 5.99;
	std::cout << b;
}

结果:

但是要怎么换行呢,我们可以直接用std::endl这是一个函数。


std::endl换行

相当于插⼊⼀个换⾏字符加刷新缓冲区

int main()
{
	// << 流插入
	std::cout << "您好" << std::endl;
	int a = 10;
	std::cout << a << std::endl << std::endl;

	double b = 5.99;
	std::cout << b << std::endl;
}


 std::cin的标准输⼊流

std::cin的标准输⼊流,它就是把在屏幕(终端)上输入的字符转换成对应的整行浮点型,给给变量。

int main()
{
	int a = 0;
	double b = 10;
	//输入流
	std::cin >> a >> b;
	//输出流
	std::cout << a << " " << b << std::endl;

}

输入了5和9.9,输出了5和9.9。


可以把cout和cin暴露出来,这样就不用在前面加std::了


#include<iostream>
using namespace std;
int main()
{
	// 在io需求⽐较⾼的地⽅,如部分⼤量输⼊的竞赛题中,加上以下3⾏代码
	// 可以提⾼C++IO效率
	ios_base::sync_with_stdio(false);//这一句是让c++不在兼容c语言,关掉
	cin.tie(nullptr);//不在跟其他流绑定,自己做自己的
	cout.tie(nullptr);//不在跟其他流绑定,自己做自己的
	return 0;
}

缺省参数


缺省参数就是在行参里给一个赋值,就是缺省参数,

不传参时,使用的就是缺省参数,传参时,使⽤指定的实参。

#include <iostream>
#include <assert.h>
using namespace std;

void fang(int a = 0)//缺省参数
{
	cout << a << endl;
}
int main()
{
	fang();// 没有传参时,使⽤参数的默认值
	fang(10); // 传参时,使⽤指定的实参
	return 0;
}

 全缺省

全缺省就是全部都是缺省参数不传参时,使用的就是缺省参数,传参时,使⽤指定的实参,

传1的时候,a就是1了,传1和2的时候,a就是1,b就是2,传1,2,3的话,a是1,b是2,c是3。

#include <iostream>
using namespace std;
// 全缺省
void Func1(int a = 10, int b = 20, int c = 30)
{
	cout << "a = " << a << endl;
	cout << "b = " << b << endl;
	cout << "c = " << c << endl << endl;
}
int main()
{
	Func1();
	Func1(1);
	Func1(1, 2);
	Func1(1, 2, 3);
	return 0;
}


半缺省

半缺省我们可以看到行参a没有被赋值,b和c都被赋值了,这就是半缺省。

#include <iostream>
using namespace std;

// 半缺省
void Func2(int a, int b = 10, int c = 20)
{
	cout << "a = " << a << endl;
	cout << "b = " << b << endl;
	cout << "c = " << c << endl << endl;
}
int main()
{
	Func2(100);
	Func2(100, 200);
	Func2(100, 200, 300);
	return 0;
}


缺省参数不能声明和定义同时给,能在声明给。

// Stack.h
#include <iostream>
#include <assert.h>
using namespace std;
typedef int STDataType;
typedef struct Stack
{
	STDataType* a;
	int top;
	int capacity;
}ST;
void STInit(ST* ps, int n = 4);
// Stack.cpp
#include"Stack.h"
// 缺省参数不能声明和定义同时给
void STInit(ST* ps, int n)
{
	assert(ps && n > 0);
	ps->a = (STDataType*)malloc(n * sizeof(STDataType));
	ps->top = 0;
	ps->capacity = n;
}
// test.cpp
#include"Stack.h"
int main()
{
	ST s1;
	STInit(&s1);
	// 确定知道要插⼊1000个数据,初始化时⼀把开好,避免扩容
	ST s2;
	STInit(&s2, 1000);
	return 0;
}

 函数重载

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(double left, double right)" << endl;
	return left + right;
}

int main()
{
	Add(10, 20);
	Add(10.1, 20.2);
	return 0;
}


参数个数不同

参数的个数不同,也可以找到对应的函数。

#include<iostream>
using namespace std;

// 2、参数个数不同
void f()
{
	cout << "f()" << endl;
}
void f(int a)
{
	cout << "f(int a)" << endl;
}

int main()
{

	f();
	f(10);

	return 0;
}


参数的顺序不同

参数的顺序不同,也可以找到对应的函数。

#include<iostream>
using namespace std;

// 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;
}

int main()
{
	f(10, 'a');
	f('a', 10);
	return 0;
}


返回函数不同不是函数重载

返回函数不同不是函数重载,因为调⽤时也⽆法区分

// 返回值不同不能作为重载条件,因为调⽤时也⽆法区分
void fxx()
{
}

int fxx()
{
 return 0;
}

下⾯两个函数构成重载,个数不同
但是f()调⽤时,会报错,存在歧义,编译器不知道调⽤谁。

构成重载,但是不能调用。

// 下⾯两个函数构成重载
// f()但是调⽤时,会报错,存在歧义,编译器不知道调⽤谁
void f1()
{
	cout << "f()" << endl;
}
void f1(int a = 10)
{
	cout << "f(int a)" << endl;
}

引⽤

引⽤的概念和定义

引⽤不是新定义⼀个变量,⽽是给已存在变量取了⼀个别名,编译器不会为引⽤变量开辟内存空间,它和它引⽤的变量共⽤同⼀块内存空间。⽐如:⽔虎传中李逵,宋江叫"铁⽜",江湖上⼈称"⿊旋⻛";林冲,外号豹⼦头;

C++中为了避免引⼊太多的运算符,会复⽤C语⾔的⼀些符号,⽐如前⾯的<<?和?>>,这⾥引⽤也和取地址使⽤了同⼀个符号&,⼤家注意使⽤⽅法⻆度区分就可以。(吐槽⼀下,这个问题其实挺坑的,个⼈觉得⽤更多符号反⽽更好,不容易混淆)


下面这代码我们可以看到,给a变量取别名为b和c,

也可以给别名b取别名,g相当于还是a的别名,g++相当于a++,

也就是a这一块空间地址,有很多个名字。,它们都是同一块空间。

#include <iostream>
using namespace std;

int main()
{
	int a = 0;
	// 引⽤:b和c是a的别名
	int& b = a;
	int& c = a;
	// 也可以给别名b取别名,g相当于还是a的别名
	int& g = b;
	g++;

	// 这⾥取地址我们看到是⼀样的
	cout << a << endl;
	cout << b << endl;
	cout << c << endl;
	cout << g << endl;

	return 0;
}


引⽤的特性

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

当然指针也是可以取别名的,p1取了a的地址,p1的别名是p2,p2也就是a的地址,

p2 = &e 这个,p2本来是指向a的,现在被指向e了,所以打印出了的是99。

int main()
{
	int a = 1;
	int* p1 = &a;
	int*& p2 = p1;

	int e = 99;
	p2 = &e;

	cout << *p2 << endl;
	return 0;
}


 引⽤的使⽤


我们可以看到jh这个函数,传了a和b过去,x取的是a的别名,y取的是b的别名,所以交换还是a和b。

#include <iostream>
using namespace std;

//交换
void jh(int& x, int& y)
{
	int tab = x;
	x = y;
	y = tab;
}

int main()
{
	int a = 10;
	int b = 99;
	cout << "没交换前:" << a << " " << b << endl;
	//交换函数
	jh(a, b);
	cout << "交换后:" << a << " " << b << endl;
	
}

结果:

我们可以看到语句交换了


#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");
			return;
		}
		rs.a = tmp;
		rs.capacity = newcapacity;
	}
	rs.a[rs.top] = x;
	rs.top++;
}
// 栈顶
int& STTop(ST& rs)
{
	assert(rs.top > 0);
	return rs.a[rs.top];
}
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;
}

我们这里+10的话,加的是临时对象,那要怎么解决这种问题呢?


那就要用到引⽤返回了,这样返回的就是别名了,加的也是数组里的数值了。

结果:


引用返回在有些场景是不能用的,下面这一张图我们可以看到吗,a引用返回,但是局部空间都销毁,加10就越界访问了。



#include<iostream>
using namespace std;
typedef struct SeqList
{
	int a[10];
	int size;
}SLT;
// ⼀些主要⽤C代码实现版本数据结构教材中,使⽤C++引⽤替代指针传参,⽬的是简化程序,避开复
//杂的指针,但是很多同学没学过引⽤,导致⼀头雾⽔。
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));
	newnode->val = x;
	newnode->next = NULL;
	if (phead == NULL)
	{
		phead = newnode;
	}
	else
	{
		//...
	}
}

int main()
{
	PNode plist = NULL;
	ListPushBack(plist, 1);
	return 0;
}

const引⽤

权限不能放大,我们可以看到被const修饰的变量,就不能在引用了,const引用就可以了。


权限可以缩小,被const引用的ps不能修改,但是b可以。


int main()
{
	// 权限不能放大
	const int a = 10;
	const int* p1 = &a;
	//int* p2 = p1;

	// 权限可以缩小
	int b = 20;
	int* p3 = &b;
	const int* p4 = p3;

	// 不存在权限放大,因为const修饰的是p5本身不是指向的内容
	int* const p5 = &b;
	int* p6 = p5;

	return 0;
}

rd引用的是临时变量,所以需要const引用

#include<iostream>
using namespace std;
int main()
{
    //a是先给到临时变量,再给到rd , 临时变量是被cinst修饰的
    //rd引用的是临时变量,所以需要cinst引用

	int a = 10;
	const int& ra = 30;
	// 编译报错: “初始化”: ⽆法从“int”转换为“int &”
	// int& rb = a * 3;
    //加上const就行了
	const int& rb = a * 3;



    //d是先给到临时变量,再给到rd , 临时变量是被cinst修饰的
    //rd引用的是临时变量,所以需要cinst引用

	double d = 12.34;
	// 编译报错:“初始化”: ⽆法从“double”转换为“int &”
	// int& rd = d;
    //加上const就行了,
	const int& rd = d;
	return 0;
}

引用传参

const可以引用以下对象

1.const对象

2.普通对像

3.临时对象

void f1(const int& rx)
{

}

int main()
{
	const int xx = 20;
	int a = 10;
	const int& rb = a * 3;

	double d = 12.34;
	const int& rd = d;

	f1(xx);
	f1(a);
	f1(a*3);
	f1(d);

	return 0;
}

指针和引⽤的关系

C++中指针和引⽤就像两个性格迥异的亲兄弟,指针是哥哥,引⽤是弟弟,在实践中他们相辅相成,功能有重叠性,但是各有⾃⼰的特点,互相不可替代。

  • 语法概念上引⽤是⼀个变量的取别名不开空间,指针是存储⼀个变量地址,要开空间。
  • 引⽤在定义时必须初始化,指针建议初始化,但是语法上不是必须的。
  • 引⽤在初始化时引⽤⼀个对象后,就不能再引⽤其他对象;⽽指针可以在不断地改变指向对象。
  • 引⽤可以直接访问指向对象,指针需要解引⽤才是访问指向对象。
  • sizeof中含义不同,引⽤结果为引⽤类型的⼤⼩,但指针始终是地址空间所占字节个数(32位平台下占4个字节,64位下是8byte)
  • 指针很容易出现空指针和野指针的问题,引⽤很少出现,引⽤使⽤起来相对更安全⼀些。

inline展开


⽤inline修饰的函数叫做内联函数,编译时C++编译器会在调⽤的地⽅展开内联函数,这样调⽤内联函数就不需要建⽴栈帧了,就可以提⾼效率。

#include<iostream>
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))
// 为什么不能加分号?
// 为什么要加外⾯的括号?
// 为什么要加⾥⾯的括号?
int main()
{
	int ret = ADD(1, 2);
	cout << ADD(1, 2) << endl;
	cout << ADD(1, 2) * 5 << endl;
	int x = 1, y = 2;
	ADD(x & y, x | y); // -> (x&y+x|y)
	return 0;
}

inline不建议声明和定义分离到两个⽂件,分离会导致链接错误。因为inline被展开,就没有函数地址,链接时会出现报错。

// F.h
#include <iostream>

using namespace std;
inline void f(int i);


// F.cpp--错误
#include "F.h"
void f(int i)
{
	cout << i << endl;
}

// F.cpp--正确
#include "F.h"
inline void f(int i)
{
	cout << i << endl;
}


// main.cpp
#include "F.h"
int main()
{
	// 链接错误:⽆法解析的外部符号 "void __cdecl f(int)" (?f@@YAXH@Z)
	f(10);
	return 0;
}

nullptr

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

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

C++是0;

C语言是((void *)0);

#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(int x),因此与程序的初衷相悖。
	f(NULL);
	f((int*)NULL);
	// 编译报错:error C2665: “f”: 2 个重载中没有⼀个可以转换所有参数类型
	// f((void*)NULL);
	f(nullptr);
	return 0;
}


举报

相关推荐

0 条评论