第四章 复合类型

时间:2023-01-07 15:29:42
数组
数组大小的计算:
sizeof 数组名 而不是sizeof 数组名[]
数组的某一个元素大小使用 sizeof 数组名[n]
sizeof的返回值为整型的byte值
字符串

字符串相加实现如图:
strcat() 函数用来连接字符串,其原型为:
    char *strcat(char *dest, const char *src);
【参数】dest 为目的字符串指针,src 为源字符串指针。
strcat() 会将参数 src 字符串复制到参数 dest 所指的字符串尾部;dest 最后的结束字符 NULL 会被覆盖掉,并在连接后的字符串的尾部再增加一个 NULL。
注意:dest 与 src 所指的内存空间不能重叠,且 dest 要有足够的空间来容纳要复制的字符串。
另外的实现方法有:
strlen()函数相关的注意事项:

字符串表示的又一种表示:
结构体


顺便说一下,访问类的成员函数(如cin.getline())的方式是从访问结构成员变量(如vincent.price)的方式衍生而来的。
结构体的声明举例如下:
// structur.cpp -- a simple structure
#include <iostream>
struct inflatable   // structure declaration
{
    char name[20];
    float volume;
    double price;
};
int main()
{
    using namespace std; 
    inflatable guest =
    {
        "Glorious Gloria",  // name value
        1.88,               // volume value
        29.99               // price value
    };  // guest is a structure variable of type inflatable
// It's initialized to the indicated values
    inflatable pal =
    {
        "Audacious Arthur",
        3.12,
        32.99
    };  // pal is a second variable of type inflatable
// NOTE: some implementations require using
// static inflatable guest =
    cout << "Expand your guest list with " << guest.name;
    cout << " and " << pal.name << "!\n";
// pal.name is the name member of the pal variable
    cout << "You can have both for $";
    cout << guest.price + pal.price << "!\n";
    // cin.get();
    return 0;
}
结构初始化:


然而把结构的声明和结构体变量的初始化分开更易于阅读和理解
结构数组的声明与基本的数组声明和使用无太大差异和区别!
共用体
共用体常用语(但并非只能用于)节省内存;另外还常用于操作系统数据结构或硬件数据结构。
枚举

指针和*存储空间
计算机程序在存储数据时必须跟踪的3种基本属性:
信息存储在何处、信息的值为多少、存储的信息是什么类型。
指针:




指针应用举例:
// use_new.cpp -- using the new operator
#include <iostream>
int main()
{
    using namespace std;
    int nights = 1001;
    int * pt = new int;         // allocate space for an int
    *pt = 1001;                 // store a value there
    cout << "nights value = ";
    cout << nights << ": location " << &nights << endl;
    cout << "int ";
    cout << "value = " << *pt << ": location = " << pt << endl;
    double * pd = new double;   // allocate space for a double
    *pd = 10000001.0;           // store a double there
    cout << "double ";
    cout << "value = " << *pd << ": location = " << pd << endl;
    cout << "location of pointer pd: " << &pd << endl;
    cout << "size of pt = " << sizeof(pt);
    cout << ": size of *pt = " << sizeof(*pt) << endl;
    cout << "size of pd = " << sizeof pd;
    cout << ": size of *pd = " << sizeof(*pd) << endl;
     cin.get();
    return 0;
}
程序运行结果:




不要尝试释放已经释放的内存块,C++标准指出,这样做的结果将是不确定的,这意味着什么情况都可能发生。另外,不能使用delete来释放声明变量所获得的内存,主要是用于释放new声明的内存。

指针数组的声明:

使用new和delete注意事项:

不能使用sizeof运算符来确定动态分配的数组包含的字节数。
指针数组应用举例:
体现new声明的动态数组的使用便捷之处
#include <iostream> 
int main()
{
    using namespace std;
    double * p3 = new double [3]; // space for 3 doubles
    p3[0] = 0.2;                  //  使用指针P3直接作为数组名称就可以操作数组了
    p3[1] = 0.5;
    p3[2] = 0.8;
    cout << "p3[1] is " << p3[1] << ".\n";
    p3 = p3 + 1;                  // increment the pointer
    cout << "Now p3[0] is " << p3[0] << " and ";
    cout << "p3[1] is " << p3[1] << ".\n";
    p3 = p3 - 1;                  // point back to beginning
    delete [] p3;                 // free the memory
     cin.get();
    return 0;
}



指针与字符串
char animal[20] = "bear"; //将b/e/a/r分别存储在                             animal[0...3]
程序分析:
// ptrstr.cpp -- using pointers to strings
#include <iostream>
#include <cstring>              // declare strlen(), strcpy()
int main()
{
    using namespace std;
    char animal[20] = "bear";   // animal holds bear
    const char * bird = "wren"; // 声明指向char的const指针bird并初始化为“wren”
    char * ps;                  // 声明指向char的指针ps
    cout << animal << " and ";  // display bear
    cout << bird << "\n";       // 指针bird直接作数组名使用
    // cout << ps << "\n";      //may display garbage, may cause a crash
    cout << "Enter a kind of animal: ";
    cin >> animal;              // ok if  input < 20 chars
    // cin >> ps; Too horrible a blunder to try; ps doesn't
    //            point to allocated space
    ps = animal;                // set ps to point to string
    cout << ps << "!\n";       // ok, same as using animal
       
    cout << "Before using strcpy():\n";
    cout << animal << " at " << (int *) animal << endl;
    //输出animal的首地址或者是
    cout << ps << " at " << (int *) ps << endl;
    ps = new char[strlen(animal) + 1];  // get new storage
//strlen()计算数组长度,不会计算空字符长度,所以需要+1才能够存储下原有的animal数组
    strcpy(ps, animal);         // copy string to new storage
        strcpy(s1,s2); strcpy函数的意思是:把字符串s2中的内容copy到                   s1中,连字符串结束标志也一起copy.
    cout << "After using strcpy():\n";
    cout << animal << " at " << (int *) animal << endl;
    cout << ps << " at " << (int *) ps << endl;
    delete [] ps;    //销毁动态申请的动态数组内存
     cin.get();
     cin.get();
    return 0;
}
动态结构(体)
应用举例如下:
#include <iostream>
struct inflatable   // structure inflatable definition
{
    char name[20];
    float volume;
    double price;
};
int main()
{
    using namespace std;
    inflatable * ps = new inflatable; // allot memory for structure
    cout << "Enter name of inflatable item: ";
    cin.get(ps->name, 20);            // method 1 access member access
    cout << "Enter volume in cubic feet: ";
    cin >> (*ps).volume;              // method 2 access member access
    cout << "Enter price: $";
    cin >> ps->price;
    cout << "Name: " << (*ps).name << endl;              //             method 2
    cout << "Volume: " << ps->volume << " cubic feet\n"; // method 1
    cout << "Price: $" << ps->price << endl;             // method 1
    delete ps;                        // free memory used by structure
     cin.get();
    
 cin.get();
    return 0;
}
自动存储、静态存储和动态存储(C++11新增了线程存储)
数组的替代品(模板类vector和模板类array)
vector:    n_elem可以为变量
array:
总结