C++复习笔记2

        引用:引用不是新定义一个变量,而是给已存在的变量起了一个别名,编译器并不开辟空间,它和它引用的变量共用同一块内存空间。显然引用的数据类型必须与被引用变量的数据类型一致。引用底层是一个指针常量,它不能修改指向。引用传参或者返回能够提高效率。

      注意:不要返回临时变量的引用或者地址。

       1.引用必须初始化,没有空引用。

       2.引用定义之后,不能修改指向。

       3.一个变量可以有多个引用      

      4 记住数组的引用写法,指针的引用写法。

#include<iostream>
using namespace std;


int main()
{

	int a = 10;
	//int& a;//空引用不存在
	int& b = a;


system("pause");
return 0;
}

地址显示:

通过引用改变变量的值,由于指向同一块空间,所有的值都被修改了。

#include<iostream>
using namespace std;


int main()
{

	int a = 10;
	//int& a;//空引用不存在
	int& b = a;

	b = 20;
	cout << a << endl;

system("pause");
return 0;
}

           数组引用写法比较特殊,需要记住。指针的引用与原指针的值相等,(意思是指向同一块空间),解引用的值也相等,取地址的值也相等(说明了别名性质)。

#include<iostream>
using namespace std;

void test01()
{
	int a = 10;//变量的引用
	int& b = a;

	int ar[10] = { 1,2,3,4,5,6,7,8,9,10 };//数组的引用
	int (&br)[10] = ar;//数组引用的写法要记住


	int* ptr = &a;
	int*& q = ptr;
	cout << ptr << endl;
	cout << q << endl;
}


int main()
{

	int a = 10;
	//int& a;//引用不能为空
	int& b = a;
	int& c = b;
	int& d = a;

	int x = 20;
	//int& b = x;//引用不能修改指向
	//&b = x;//引用不能修改指向

	b = x;//赋值操作


	b = 20;
	cout << a << endl;
	test01();

system("pause");
return 0;
}

引用传参,典型应用变量交换。

#include<iostream>
using namespace std;

struct Student
{
	char name[10];
	char sex[5];
	int age;
};

void swap(int& a, int b)
{
	int temp = a;
	a = b;
	b = temp;
}

void swap(int* a, int* b)
{
	int* temp = a;
	a = b;
	b = temp;
}

void swap2(int* a, int* b)
{
	int temp = *a;
	*a = *b;
	*b = temp;
}

void test01()
{
	int a = 10;
	int& b = a;

	int c = 20;
	b = c;//赋值操作
}

void test02()
{
	int arr[] = { 1,2,3,4,5 };
	int(&brr)[5] = arr;//数组引用写法 固定有一个小括号

	int* ptr = arr;
	int*& q = ptr;
	
	cout << ptr << endl;
	cout << q << endl;

	cout << &ptr << endl;
	cout << &q << endl;

	Student stu;
	Student& rstu = stu;
}

void test03()
{
	int a = 10;
	const int& b = a;
	a = 20;
    
	//b = 30;//常引用 相当于const int* const b;

	const int c = 30;
	//int& d = c;//给了d修改c的机会,报错
	const int& d = c;//俩都不能改
}

int  main()
{
	//test01();
	test02();

	int a = 10;
	int b = 20;

	//swap(a,b);
	//cout << "a = " << a <<" b = " << b << endl;

	//swap(&a, &b);
	//cout << "a = " << a << " b = " << b << endl;

	swap2(&a, &b);
	cout << "a = " << a << " b = " << b << endl;
	system("pause");
	return 0;
}

引用用于函数返回值,注意不要返回临时变量的引用。

int& func(int a, int b)
{
    int v = a + b;
	return v;
}

void test04()
{
	int& res = func(10, 20);
	cout << res << endl;
	func(1, 2);
	cout << res << endl;
}

        局部的静态变量存储在全局区,生命周期是整个程序运行期间,不会随着函数执行结束而销毁,可以使用引用返回。

int& func(int a, int b)
{
   static  int v = a + b;
	return v;
}

void test04()
{
	int& res = func(10, 20);
	cout << res << endl;
	func(1, 2);
	cout << res << endl;
}

 传引用和传值性能测试。

#include<iostream>
using namespace std;

struct Student
{
	char name[10];
	int age;
	int id;
	char sex[5];
};

struct Test
{
	Student arr[10000];
};

void ValueTest(Test t) {}
void RefTest(Test& t) {}

Test t;

void TestValueTime()
{
	time_t begin = clock();
	for (int i = 0; i < 10000; ++i)
	{
		ValueTest(t);
	}
	time_t end = clock();
	cout << "value time = " << end - begin << endl;
}

void TestRefTime()
{
	time_t  begin = clock();
	for (int i = 0; i < 10000; ++i)
	{
		RefTest(t);
	}
	time_t end = clock();
	cout << "ref time = " << end - begin << endl;
}

int main()
{
	TestValueTime();
	TestRefTime();
}

 引用返回和值返回性能测试。

#include<iostream>
using namespace std;

struct Student
{
	char name[10];
	int age;
	int id;
	char sex[5];
};

struct Test
{
	Student arr[10000];
};

Test t;

//void ValueTest(Test t) {}
//void RefTest(Test& t) {}

Test fun1()
{
	return t;
}

Test& fun2()
{
	return t;
}

void TestValueTime()
{
	time_t begin = clock();
	for (int i = 0; i < 10000; ++i)
	{
		//ValueTest(t);
		fun1();
	}
	time_t end = clock();
	cout << "value time = " << end - begin << endl;
}

void TestRefTime()
{
	time_t  begin = clock();
	for (int i = 0; i < 10000; ++i)
	{
		//RefTest(t);
		fun2();
	}
	time_t end = clock();
	cout << "ref time = " << end - begin << endl;
}

int main()
{
	TestValueTime();
	TestRefTime();
}

 引用和指针的区别。

1.引用必须初始化,指针没有要求

2 引用不能修改指向,指针可以

3.没有空引用,有空指针

4 sizeof大小不同,引用结果为引用类型大小,指针结果为地址空间所占字节数

5 ++操作结果不同,引用是值增加,指针向后偏移一个类型大小

6.有多级引用,没有多级指针

7指针解引用访问数据,引用直接访问

8 引用比指针用起来更安全

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值