• C++ Primer Plus读书笔记(三)复合类型


    这节主要是介绍数组、结构体、类等概念,以及new delete内存管理。

    1、数组

      首先普及一下C++中字符串的概念,只有在结尾有 0 的才叫字符串, cout 输出字符串也以空字符为标记进行结束输出。

    2、输入

      读取一行数据,有两个函数可以实现

    1 cin.get();
    2 cin.getline();

      其中cin.getline()会把末尾的空字符丢掉。读取指定长度可以这么用 cin.getline(name, LENGTH); 

    3、string类

      注意下边这种用法,这是明显区分与C语言的字符串数组的特性。

    1 string str1;
    2 string str2;
    3 string str3;
    4 
    5 str1 = str2 + str3;

    4、结构体

      这个和C语言没什么区别。

    5、指针

      1 - new的用法

    1 type_name* point_name = new type_name;
    2 
    3 
    4 int* array = new int [10];
    5 delete [] array;    //对应的释放

      2 - delete

    1 int* ptr = new int;
    2 delete ptr;

      需注意几点:

        a. 只能释放new出来的内存,delete参数是地址(ptr而不是*ptr)

        b. 可以释放空指针

      3、C++将数组名解释为数组第一个元素的地址:

    1 double wages[10] = {};
    2 
    3 wages == &wages[0]

       

      4、关于指针数组

     1 struct year_end
     2 {
     3     int year;
     4     //some else 
     5 };
     6 
     7 struct year_end s1, s2, s3;
     8 struct year_end * arp[3] = {&s1, &s2, &s3};
     9 
    10 struct year_end **arp2 = arp;
    11 auto ppb = arp;    //use auto

      5、摸板类vector

        数组的替代品,使用new和delete自动进行内存管理。用法举例:

    1 #include <vector>
    2 
    3 vector<int> vint;    //一个int型的vector对象
    4 vector<double> vdouble(n);    //一个double型的vector对象

      5、模板类array

    1 #include <array>
    2 
    3 using namespace std;
    4 
    5 array<int, 5> array_int;
    6 array<double, 10> array_double = {1.1, 2.2, ...};
    7 
    8 array<type_name, number> array_name;

        ps:number不能是变量,只能是常量

      访问vector或者array时,可以使用自身的成员函数 at ,有检查越界的保护,举例:

    1 array_double.at(1);
    2 
    3 array_double.begin();
    4 array_double.end();
    5 //确定边界的成员函数,后边再说
  • 相关阅读:
    cocos2dx中的精灵CCSprite
    Mark Russinovich 的博客:Windows Azure 主机更新:原因、时间和方式
    cocos2d-x中的尺寸之三
    cocos2d-x中的尺寸之二
    cocos2d-x中的尺寸之一
    cocos2d-x中的导演、场景、层和精灵
    宣布正式发布 Windows Azure 上的 Oracle 软件以及 Windows Azure Traffic Manager 更新
    Hello China操作系统STM32移植指南(三)
    Hello China操作系统STM32移植指南(二)
    diff_mysql_table_exec.py
  • 原文地址:https://www.cnblogs.com/gaoshaonian/p/12358001.html
Copyright © 2020-2023  润新知