C++ 引用 ‘&’

C++新增了一种复合类型——引用变量。引用是已定义变量的一个别名。

引用变量的主要用途是用作函数的形参。通过将引用变量用作参数,函数将使用原始数据,而不是其副本。

例如代码中的swap函数。


1.实现字符串数组的引用

#include<iostream>

using namespace std;

//实现字符串数组的引用

void main1()
{
	char a[3][10]{"hello", "world", "china"};
	char(& ra)[3][10](a);
	for (auto i : ra)
	{
		cout << i << endl;
	}
	cin.get();
}

void main()
{
	char *a[3]{"hello", "world", "china"};
	char * (&ra)[3](a);
	for (auto i : ra)
	{
		cout << i << endl;
	}
	cin.get();
}

2.实现字符串二维数组的引用


<pre name="code" class="cpp">#include<iostream>

using namespace std;

void main()
{
	char *a[3][10]{{ "hello", "world", "china" }, { "Obama", "Opera", "Modi" }, { "lady", "gaga", "justin" }};
	char * (&ra)[3][10](a);
	/*for (auto i : ra)
	{
		cout << *i << "  " << *(i + 1) << "   " << *(i + 2) << endl;
	}*/
	
	for (auto i : ra)
	{
		for (int j = 0; j < 3; j++)
		{
			cout << *(i + j) << "  ";
		}
		cout << endl;
	}	
	cin.get();
}


 

3.实现函数指针的引用

<pre name="code" class="cpp">#include<iostream>

using namespace std;

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

void main()
{
	int a = 2, b = 3;
	int c = 10, d = 12;

	cout << a << "  " << b << endl;
	swap(a, b);
	cout << a << "  " << b << endl;
	void(*p)(int &, int &)(swap);
	void(*&rp)(int &, int &)(p);
	cout << c << "  " << d << endl;
	rp(c, d);
	cout << c << "  " << d << endl;

	cin.get();
}


 

4.实现二级函数指针的引用


<pre name="code" class="cpp">#include<iostream>

using namespace std;

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

void main()
{
	int a = 2, b = 3;
	void(*p)(int &, int &)(swap);
	void(**pp)(int &, int &)(&p);
	void(** & rpp)(int &, int &)(pp);
	cout << "数据交换之前" << a << "  " << b << endl;
	(*pp)(a, b);
	cout << "数据交换之后" << a << "  " << b << endl;

	cin.get();
}


 

5.实现函数指针数组的引用

<pre name="code" class="cpp">#include<iostream>

using namespace std;

int add(int a, int b)
{
	return a + b;
}

int sub(int a, int b)
{
	return a - b;
}

int mul(int a, int b)
{
	return a*b;
}

int divi(int a, int b)
{
	return a / b;
}

void main()
{
	int a = 100, b = 10;
	int(*p[4])(int a, int b){ add, sub, mul, divi };
	int(*(& rp)[4])(int a, int b)(p);
	for (auto i : rp)
	{
		cout << i(a, b) << endl;
	}

	cin.get();
}


 
6.实现三维数组的引用


#include <iomanip>
#include<iostream>

using namespace std;

void main()
{
	int a[2][3][4]{{ { 1, 2, 3, 4 }, { 5, 6, 7, 8 }, { 9, 10, 11, 12 } }, { { 13, 14, 15, 16 }, { 17, 18, 19, 20 }, { 21, 22, 23, 24 } }};
	int (& ra)[2][3][4](a);
	cout.flags(ios::left);
	for (int i = 0; i < 2; i++)
	{
		for (int j = 0; j < 3; j++)
		{
			cout << endl;
			for (int k = 0; k < 4; k++)
			{
				cout << setw(5) << ra[i][j][k];
			}
		}
		cout << endl;
		cout << endl;
	}

	cin.get();
}



7.实现参数与返回值都是函数指针的函数的引用


#include<iostream>

using namespace std;

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

void(*& callfun(void(* & rp)(int &,int &)))(int & a,int & b)
{
	p = rp;
	return p;
}

void main()
{
	int a = 2, b = 3;

	cout << "数据交换之前" << a << "   " << b << endl;
	void(*& newfun)(int &, int &)(callfun(p));
	newfun(a, b);
	cout << "数据交换之后" << a << "   " << b << endl;

	cin.get();
}




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值