示例:
1.C++中的bool类型示例
#include <iostream>using namespace std;int main()
{bool b1=10;bool b2=0;bool b3=-10;cout << "b1=" << b1 << endl; //1cout << "b2=" << b2 << endl; //0cout << "b3=" << b3 << endl; //1bool b4=true;bool b5=false;cout << "b4=" << b4 << endl; //1cout << "b5=" << b5 << endl; //0cout << boolalpha << "b1=" << b1 << endl; //truecout << boolalpha << "b2=" << b2 << endl; //falsecout << noboolalpha << "b4=" << b4 << endl; //1cout << noboolalpha << "b5=" << b5 << endl; //0return 0;
}
运行结果:
2.引用(reference)示例
#include <iostream>using namespace std;int main()
{int num = 520; //定义一个普通变量//int &ref; //定义引用时,如果不初始化,直接报错int &ref1 = num; //此时ref1引用的目标为num//引用与其目标的数据是同步的cout<<"num = "<<num<<" ref1 = "<<ref1<<endl; //对数据可读num = 1314; //更改目标, 引用跟着变化cout<<"num = "<<num<<" ref1 = "<<ref1<<endl; //对数据可读ref1 = 999; //更改引用, 目标跟着改变 对数据可写cout<<"num = "<<num<<" ref1 = "<<ref1<<endl; //对数据可读//引用与其目标的地址是一致的cout<<"&num = "<<&num<<" &ref1 = "<<&ref1<<endl; //地址相同//引用与其目标内存大小是一致的cout<<"sizeof num = "<<sizeof(num)<<" sizeof ref1 = "<<sizeof(ref1)<<endl; //对数据可读//引用与其目标的类型是一致的cout<<"typeid.name num = "<<typeid(num).name()<<" typeid.name ref1 = "<<typeid (ref1).name()<<endl;//验证引用的目标一旦指定,后期就无法进行更改int key = 333; //新的普通变量ref1 = key; //该语句不报错,执行的结果为将key的值,赋值给ref1也就是赋值给num//&ref1 = key; //给地址赋值,报错//int &ref1 = key; //引用重定义//一个目标可以定义多个引用int &ref2 = num;cout<<"&num = "<<&num<<" &ref1 = "<<&ref1<<" &ref2 = "<<&ref2<<endl; //地址相同//可以给 引用定义一个引用int &ref3 = ref1;cout<<"&num = "<<&num<<" &ref1 = "<<&ref1<<" &ref2 = "<<&ref2<<" &ref3 = "<<&ref3<<endl; //地址相同//引用与引用的目标类型不一致时会报错//double &ref4 = num;//定义指针的引用int *ptr = # //定义一个指针int * &ptrRef = ptr; //定义的是指针的引用cout<<"num = "<<num<<" *ptr = "<<*ptr<<" *ptrRef = "<<*ptrRef<<endl;//定义数组的引用int arr[5] = {1,2,3,4,5};int (&arrRef)[5] = arr;
// for(int val:arrRef)
// {
// cout<<val<<" ";
// }for(int i=0; i<5; i++){cout<<arrRef[i]<<" "; //cout<<arr[i]<<" ";}cout<<endl;cout<<"sizeof arrRef = "<<sizeof (arrRef)<<endl; //20return 0;
}
运行结果:
3.引用作为函数参数示例
#include <iostream>using namespace std;//自定义交换函数1
void swap_1(int n, int k)
{//交换三部曲int temp = n;n = k;k = temp;cout<<"swap_1:: n = "<<n<<" k = "<<k<<endl; //1314 520
}//自定义交换函数2
void swap_2(int *ptr, int *qtr)
{//交换三部曲int *temp = ptr;ptr = qtr;qtr = temp;cout<<"swap_2::*ptr = "<<*ptr<<" *qtr = "<<*qtr<<endl; //1314 520
}//自定义交换函数3
void swap_3(int *ptr, int *qtr)
{//交换三部曲int temp = *ptr;*ptr = *qtr;*qtr = temp;cout<<"swap_2::*ptr = "<<*ptr<<" *qtr = "<<*qtr<<endl; //1314 520
}//自定义交换函数4
void swap_4(int &n, int &k)
{//交换三部曲int temp = n;n = k;k = temp;cout<<"swap_4:: n = "<<n<<" k = "<<k<<endl; //520 1314
}int main()
{int num = 520;int key = 1314;//调用函数1,完成两个数的交换swap_1(num, key);cout<<"main:: num = "<<num<<" key = "<<key<<endl; //520 1314//调用函数2,完成两个数的交换swap_2(&num, &key);cout<<"main:: num = "<<num<<" key = "<<key<<endl; //520 1314//调用函数3,完成两个数的交换swap_3(&num, &key);cout<<"main:: num = "<<num<<" key = "<<key<<endl; //1314 520//调用函数4,完成两个数的交换swap_4(num, key);cout<<"main:: num = "<<num<<" key = "<<key<<endl; //520 1314return 0;
}
运行结果:
4.数组引用作为函数参数示例
#include <iostream>using namespace std;//定义函数进行冒泡排序
//void my_sort(int brr[], int n)
//{
// cout<<sizeof(brr)<<endl; //虽然使用的是数组名进行接收,但是本质上还是指针
//}//定义函数进行冒泡排序
//void my_sort_1(int *ptr, int n)
//{
// cout<<sizeof(ptr)<<endl; //4
//}//定义函数进行冒泡排序
void my_sort(int (&arrRef)[5])
{cout<<sizeof (arrRef)<<endl; // 接收的就是主调函数中的数组本身cout<<"arrRef = "<<arrRef<<endl; //地址for(int i=1; i<5; i++){for(int j=0; j<5-i; j++){if(arrRef[j] > arrRef[j+1]){//交换int temp = arrRef[j];arrRef[j] = arrRef[j+1];arrRef[j+1] = temp;}}}
}int main()
{int arr[5] = {3,2,1,4,5};cout<<"arr = "<<arr<<endl;//调用函数进行冒泡排序my_sort(arr);//输出排序后的结果for(int val : arr){cout<<val<<" ";}cout<<endl;return 0;
}
运行结果:
5.常引用(const)示例
#include <iostream>using namespace std;int my_max(const int &m, const int &n)
{return m>n?m:n;
}int main()
{int num = 520;int key = 1314;cout<<my_max(num, key)<<endl; //1314cout<<my_max(520, 1314)<<endl;return 0;
}
运行结果:
6.单个内存空间的申请与释放示例
#include <iostream>using namespace std;int main()
{//在堆区申请一个int类型的空间大小int *p1 = new int; //int *p1 = (int *)malloc(sizeof(int));cout<<"*p1 = "<<*p1<<endl; //随机值*p1 = 520;cout<<"*p1 = "<<*p1<<endl; //520//在堆区申请空间顺便对空间中的数据进行初始化double *p2 = new double(3.14);cout<<"*p2 = "<<*p2<<endl; //3.14//在堆区申请空间顺便对空间中的数据进行初始化double *p3 = new double{56.789};cout<<"*p3 = "<<*p3<<endl; //56.789 //释放 堆区空间delete p1;delete p2;delete p3;p1 = NULL;p2 = NULL;p3 = NULL;return 0;
}
运行结果:
7.连续空间的申请和释放示例
#include <iostream>using namespace std;int main()
{int *p1 = new int[5]; //堆区连续申请5个int类型的空间for(int i=0; i<5; i++) //如果不初始化默认为随机值{cout<<*(p1+i)<<" ";}cout<<endl;//使用堆区空间,对堆区空间进行写操作for(int i=0; i<5; i++) //如果不初始化默认为随机值{*(p1+i) = 10+i;}cout<<endl;for(int i=0; i<5; i++) //如果不初始化默认为随机值{cout<<*(p1+i)<<" ";}cout<<endl;//堆区连续申请空间并对其进行初始化int *p2 = new int[5]{5,7,3,4,8};for(int i=0; i<5; i++) //如果不初始化默认为随机值{cout<<*(p2+i)<<" ";}cout<<endl;//释放堆区空间delete []p1;delete [] p2;p1 = NULL;p2 = NULL;return 0;
}
运行结果:
8.练习:要求在堆区申请5个double类型的空间,用于存储5名学生的成绩。请自行封装函数完成
1> 空间的申请
2> 学生成绩的录入
3> 学生成绩的输出
4> 学生成绩进行降序排序
5> 释放申请的空间
主程序中用于测试上述函数
#include <iostream>using namespace std;
void shuru(double *p1){for(int i=0;i<5;i++){cout<<"shuru>>";cin>>p1[i];}
}
void shuchu(double * &p1){for(int i=0;i<5;i++){cout<<p1[i]<<" ";}
}
void paixu(double *p1,double n){for(int i=1;i<n;i++){for(int j=0;j<n-i;j++){if(p1[j]<p1[j+1]){int temp=p1[j];p1[j]=p1[j+1];p1[j+1]=temp;}}}
}int main()
{double *p1=new double [5];if(p1 == NULL){return -1;}shuru(p1);cout<<endl;shuchu(p1);paixu(p1,5);cout<<endl;shuchu(p1);delete []p1;p1=NULL;return 0;
}
运行结果: