标签:style blog http color os io 数据 for
1.通过引用来引用数组(一维数组)
#include <iostream>
#include <stdlib.h>
/*引用就是给原来的变量有一个别名同一个地址*/
void main()
{
int a[10] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
int(&ra)[10](a);
int i = 0;
for (auto data:ra) //C++11的循环
{
data = i + 5;
std::cout << data << std::endl;
}
std::cout << a << " " << ra << std::endl;
std::cout << &a << " " << &ra << std::endl;
system("pause");
}
2.引用二维数组的方式
#include <iostream>
#include <stdlib.h>
void main()
{
int a[2][5] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
//引用就是给原来的变量有一个别名同一个地址
int(&ra)[2][5](a);
for (int i = 0; i < 2;i++)
{
for (int j = 0; j < 5;j++)
{
std::cout << " " << ra[i][j];
}
std::cout << "\n";
}
std::cout << a << " " << ra << std::endl;
std::cout << &a << " " << &ra << std::endl;
system("pause");
}
3.引用函数指针
#include <iostream>
#include <stdlib.h>
int jia(int a, int b)
{
return a + b;
}
int jian(int a, int b)
{
return a - b;
}
//函数指针int (*rp)(int a,int b)
void change(int(* & rp)(int, int))
{
rp = jian;
}
int(*& changep(int(*&rp)(int, int)))(int, int)
{
rp = jian;
return rp;
}
void main()
{
int(*p)(int a, int b)(jia);
std::cout << p(1, 2) << std::endl;
change(p);
std::cout << p(1, 2) << std::endl;
std::cout << "-----------------------" << std::endl;
p = changep(p);
std::cout << p(1, 2) << std::endl;
system("pause");
}
4.数组中的元素不能为引用
5.引用的本质是指针,直接sizeof引用,就是引用的数据大小,引用变量占据4个字节
#include <iostream>
class MyClass{
char & a;
char & b;
char & c;//引用的本质是指针,直接sizeof引用,就是求引用的数据大小
//引用变量占据4个字节
};
void main()
{
int num = 10;
int & rnum(num);
double db = 10.9;
double & rdb(db); //直接作用引用的变量
std::cout << sizeof(rnum) << std::endl;
std::cout << sizeof(rdb) << std::endl;
std::cout << sizeof(MyClass) << std::endl;
system("pause");
}
运行结果如下:
#include <iostream>
int getdata(int && num)//右值引用,节约内存拷贝,内存优化所必须
{
std::cout << num << std::endl;
num += 10;
return num;
}
void main7()
{
int a = 5;
int b = 4;
std::cout << getdata(a + 1) << std::endl;
system("pause");
}
//左值,一般可以取地址就是左值
//右值某些情况可以,某些情况不可以
void main8()
{
int a = 3;
int b = a + 1;//右值->左值
std::cout << getdata(std::move(a)) << std::endl;
//std::move将左值转换为右值,C++11
}
void main9()
{
//const int num(6);
char str[10]("hello");//限定字符串不被修改
const char *pc(str);//指向常量的指针限定了指向的数据无法修改,+1,+2,+3
str[3] = ‘x‘;//可以,
//pc[3] = ‘y‘;
//*(pc + 3) = ‘y‘;
pc = "world";
system("pause");
}
void main10()
{
char str[10]("hello");
const char(&rstr)[10](str);//常量引用
const char(&rrstr)[10](rstr);//引用可以给另一个引用初始化
str[4] = ‘X‘;
//rstr[4] = ‘Y‘;
}
void main11()
{
int(*p)(int a, int b)(jia);
std::cout << p(1, 2) << std::endl;
int(*const &rp)(int a, int b)(p);//引用函数指针
//rp=jian;//()仅仅适用于初始化
}
标签:style blog http color os io 数据 for
原文地址:http://blog.csdn.net/tototuzuoquan/article/details/38628015