zoukankan      html  css  js  c++  java
  • (3)C++复合类型

     存储数据时必须跟踪的三个属性:信息储存在何处,存储的值,存储的类型

    一、数组

    #include <iostream>
    using namespace std;
    int main()
    {
        //初始化赋值1
        short a[3];
        a[0] = 1;
        a[1] = 16;
        a[2] = 90;
    
        //始化赋值2
        short b[3] = { 2,6,13 };
    
        //始化赋值3,自己计算个数
        short c[] = { 2,6,13 };
        
        //没赋值的位置会初始化为默认0
        short d[6] = { 5 };
    
        //始化赋值4,省略等号
        int e[] { 2,6,13 };
    
        cout << e[2] <<endl;
    }

     二、字符串

    1.连续字符组成德字符串

    C语言风格,末尾要加

    char a[] = { 'd','f','e','' };

    如果末尾不加 则继续打印内存随后的各个字节,直到遇见 空字符为止

        char a[] = { 'd','f','e','' ,'n' };
        cout << a <<endl;//如果打印a 得到dfe
        cout << a[4] << endl;//可以得到 n

    2.字符串常量

        char a[] = "abcde";
        cout << a <<endl;//abcde
        cout << sizeof(a) << endl;//数组的长度  6
        cout << strlen(a) << endl;//字符串的长度 5
        a[2] = '';
        cout << a << endl;//如果中间插入空字符,则程序只打印之前的字符

    3.输入

    (1)

    int main()
    {    
        char name[20];
        cin >> name;    //cin使用空白(空格、制表符、换行)来确定字符串的结束为止,并自动在结尾添加空字符
        cout << name << endl;//如果输入中加了空格,他只会打印之前的部分
    }

    而且cin不能防止输入多出20,超出后程序会引发异常

     (2)getline()可以每次读一行

    cin.getline(name,5);//读取到指定为止,并且不受空白影响

    三、string类

    C++98标准添加了string类来扩展C++库

    使用string 必须在头文件中包含

    #include <iostream>
    #include<string> //添加
    using namespace std;
    int main()
    {    
        string str = "abc";
        cout << str << endl;
    }

    四、结构

    (1)

    #include <iostream>
    #include<string>
    using namespace std;
    int main()
    {    
        struct Student
        {
            int age;
            string name;
        };
        //初始化
        Student stu = { 20,"tom" };//等号可以省略
        cout << stu.age << endl;
    }

    (2)结构数组

    //初始化
    Student stu[2]{ {20,"tom"},{19,"mali"} };
    //调用
    cout << stu[0].age << endl;

    (3)结构中的位字段

    指定占用特定位数的结构成员,使得创建与某个硬件设备上的寄存器对应的数据结构非常方便

    加冒号

        struct Card
        {
            int shanqu : 4;//占用4bit
            bool b : 1;//
        };

    位字段通常用在低级编程中

     (4)结构体指针

        Student* p = &stu;
        cout << p->age << endl;

    五、共用体

     VC++ 不能用此类型????实例化时报错

        union Student1
        {
            int age;
            string name;
        };

    六、枚举

        enum Season
        {
            Spring, Summer, Autumn, Winter
        }; 
        Season season;
        season = Spring;

    可以赋值整数,默认为0

        enum Season
        {
            Spring=1, Summer=2, Autumn=3, Winter=4
        }; 
        Season season;
        season = Spring;
        cout << season << endl;

    七、指针和自由存储空间  八、指针、数组和指针算术

    篇幅长,合并转移到新章 https://www.cnblogs.com/buchizaodian/p/11516266.html

    九、类型组合

    十、数组的替代品

    1.模板类Vector

    2.模板类array

  • 相关阅读:
    java (取文本中间)字符串之间的文本
    Mysql数据库中text还是不够
    java读取网页内容
    controller to controller
    农历类
    java.lang.RuntimeException: com.google.inject.CreationException: Unable to create injector, see the following errors
    Java中List集合去除重复数据的方法
    idea启动tomcat的中文乱码问题
    idea局域网调试 can accept external connection不可勾选
    Mysql JDBC Url参数说明useUnicode=true&characterEncoding=UTF-8
  • 原文地址:https://www.cnblogs.com/buchizaodian/p/11511256.html
Copyright © 2011-2022 走看看