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;
}