C++:布尔类型,引用,堆区空间
1.布尔类型
#include <iostream>
using namespace std;
int main()
{
bool b1=3;
bool b2=0;
cout << "b1=" <<b1<< endl;
cout << "b2=" <<b2<< endl;
cout <<boolalpha<< "b1=" <<b1<< endl;
cout <<boolalpha<< "b2=" <<b2<< endl;
cout <<noboolalpha<< "b1=" <<b1<< endl;
cout <<noboolalpha<< "b2=" <<b2<< endl;
bool b3=true;
bool b4=false;
cout << "b3=" <<b3<< endl;
cout << "b4=" <<b4<< endl;
cout <<noboolalpha<< "b3=" <<b3<< endl;
cout <<noboolalpha<< "b4=" <<b4<< endl;
return 0;
}
2.引用
#include <iostream>
using namespace std;
int main()
{
int num;
int &ref=num;
ref=50;
cout << "num=" <<num<< endl;
cout <<"num="<<ref<<endl;
cout<<"sizeof="<<sizeof (num)<<" sizeof="<<sizeof (ref)<<endl;
cout<<"typeid num"<<typeid (num).name()<<" typeid ref"<<typeid (ref).name()<<endl;
int *ptr=#
int *ptrref=ptr;
cout<<"*ptr= " <<*ptr<<"*ptrref="<<*ptrref<<endl;
return 0;
}
3.引用函数
#include <iostream>
using namespace std;
//指针函数
int *fun()
{
static int num=100;
return #
}
int &fun1()
{
static int num1=200;
return num1;
}
int main()
{
//cout<<"num="<<fun()<<endl;
//cout<<"num="<<*fun()<<endl;
//int *p=fun();
//*fun()=20;
//cout<<"*p="<<*p<<endl;
cout<<"fun1="<<fun1()<<endl;
int &ref=fun1();
ref=666;
cout<<"ref="<<ref<<endl;
return 0;
}
4.右值引用
#include <iostream>
using namespace std;
int main()
{
int num=520;
int &ref1=num;
int &&ref2=100;
int &&ref3=4+6;
int &&ref4=num+19; //右值引用
int &&ref5=move(num);
return 0;
}
5.堆区空间申请
#include <iostream>
using namespace std;
double * apply(int n)
{
double *p1=new double[n];
if(p1==NULL)
{
return NULL;
}
for(int i=0;i<n;i++)
{
//*(p1+1)=0;
p1[i]=0;
}
return p1;
}
void socore (double * p,int n)
{
int i=0;
for(i=0;i<n;i++)
{
cout<<"输入学生的成绩:";
cin >> *(p+i);
}
cout<<endl;
}
void show(double * p,int n)
{
int i=0;
cout<<"学生的成绩:"<<ends;
for(i=0;i<n;i++)
{
cout<< *(p+i)<<ends;
}
cout<<endl;
}
void sort(double * p,int n)
{
cout<<"学生成绩降序排序:"<<ends;
for(int i=1;i<n;i++)
{
for(int j=0;j<n-i;j++)
{
if(*(p+j)<*(p+j+1))
{
double temp;
temp=*(p+j);
*(p+j)=*(p+j+1);
*(p+j+1)=temp;
}
}
}
}
void res(double * & p)
{
delete []p;
p=NULL;
}
int main()
{
double *p1=apply(5);
socore (p1,5);
show(p1,5);
sort(p1,5);
show(p1,5);
res(p1);
//p1=NULL;
return 0;
}
原文地址:https://blog.csdn.net/m0_58572142/article/details/142371003
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.kler.cn/a/316802.html 如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.kler.cn/a/316802.html 如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!