当前位置: 首页 > article >正文

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


http://www.kler.cn/news/316802.html

相关文章:

  • 力扣232:用栈实现队列
  • 【Proteus仿真】基于51单片机的宠物喂食系统设计
  • JSON合并工具
  • JVM-类加载器的双亲委派模型详解
  • 前后端数据交互 笔记03(get和post方法)
  • 使用 Azure Functions 开发 Serverless 应用:详解与实战
  • LeetCode 1014. 最佳观光组合 一次遍历数组,时间复杂度O(n)
  • 【matlab】将程序打包为exe文件(matlab r2023a为例)
  • Linux文件IO(三)-Linux系统如何管理文件
  • 【基础知识】网络套接字编程
  • QT-MOC元对象系统详解
  • 【小程序】微信小程序课程 -1 安装与配置
  • 【2025】基于微信小程序的人工智能课程学习平台的设计与实现(源码+文档+解答)
  • 职业技能大赛-自动化测试笔记分享
  • while语句
  • CANdela/Diva系列8--如何生成0x27服务解锁的DLL
  • MySQL 数据库课程设计详解与操作示例
  • Java : 图书管理系统
  • ArcGIS Pro SDK (十四)地图探索 6 图形与工具
  • AIGC7: 高通骁龙AIPC开发者沙龙过程记录A
  • 力扣刷题之2398.预算内的最多机器人数目
  • Shelly实测天工的音乐创作功能,写了一首歌,来听听效果
  • 学习笔记JVM篇(四)
  • python教程修订版
  • Redis 集群策略详解
  • oracle查询历史操作记录
  • 行为型设计模式的全面解析
  • 中小企业体系技术抽象沉淀-异地灾备篇
  • Android中如何调用DLL文件
  • 通信工程学习:什么是VM虚拟机