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=&num;
     int *ptrref=ptr;

     cout<<"*ptr= " <<*ptr<<"*ptrref="<<*ptrref<<endl;


    return 0;
}

3.引用函数

#include <iostream>

using namespace std;

//指针函数
int *fun()
{
    static int num=100;
    return &num;
}


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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值