《C++PrimerPlus》第四章:复合类型

数组

在这里插入图片描述

字符串

在这里插入图片描述

读取整行cin.getline()

缓冲队列里没有回车
在这里插入图片描述
在这里插入图片描述

读取整行cin.get()

缓冲队列里有回车,第二个cin.get读出来的是回车
在这里插入图片描述

例子

// numstr.cpp -- following number input with line input
#include <iostream>
int main()
{
    using namespace std;
    cout << "What year was your house built?\n";
    int year;
    cin >> year;
    cin.get(); // 把上一行的回车读走
    cout << "What is its street address?\n";
    char address[80];
    cin.getline(address, 80);
    cout << "Year built: " << year << endl;
    cout << "Address: " << address << endl;
    cout << "Done!\n";
    // cin.get();
    return 0;
}

string类

在这里插入图片描述

// strtype4.cpp -- line input
#include <iostream>
#include <string>               // make string class available
#include <cstring>              // C-style string library
int main()
{
    using namespace std;
    string str;
    cout << "Enter another line of text:\n";
    getline(cin, str);          // 从输入流中捕获,赋值到str中
    cout << "You entered: " << str << endl;

    return 0;
}

共用体union

在这里插入图片描述
在这里插入图片描述

指针

*取值运算符
&取址运算符

// pointer.cpp -- our first pointer variable
#include <iostream>
int main()
{
    using namespace std;
    int updates = 6;        // declare a variable
    int * p_updates;        // declare pointer to an int

    p_updates = &updates;   // assign address of int to pointer

// express values two ways
    cout << "Values: updates = " << updates;
    cout << ", *p_updates = " << *p_updates << endl;

// express address two ways
    cout << "Addresses: &updates = " << &updates;
    cout << ", p_updates = " << p_updates << endl;

// use pointer to change value
    *p_updates = *p_updates + 1;
    cout << "Now updates = " << updates << endl;
    // cin.get();
    return 0; 
}

输出

E:\xy\Code\cLionCode\cmake-build-debug\cLionCode.exe
Values: updates = 6, *p_updates = 6
Addresses: &updates = 0xd124dff784, p_updates = 0xd124dff784
Now updates = 7

进程已结束,退出代码为 0

在这里插入图片描述
在这里插入图片描述

在这里插入图片描述

指针的危险

在这里插入图片描述

223323是会写入系统随机分配的地址,该地址可能不允许输入这样的数据或者不允许修改
在这里插入图片描述

指针和数字

在这里插入图片描述

使用new来分配内存

在这里插入图片描述
在这里插入图片描述
pn 存地址0x0001



// 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; // 1001 0xb29b1ff824
    cout << "int ";
    cout << "value = " << *pt << ": location = " << pt << endl; // 1001 0x1dbcba86b70

    double * pd = new double;   // allocate space for a double
    *pd = 10000001.0;           // store a double there

    cout << "double ";
    cout << "value = " << *pd << ": location = " << pd << endl; // 10000001.0 0x1dbcba86b90
    cout << "location of pointer pd: " << &pd << endl; // 0xb29b1ff818




    cout << "size of pt = " << sizeof(pt); // 8 对应64位操作系统
    cout << ": size of *pt = " << sizeof(*pt) << endl; // 4 new int
    cout << "size of pd = " << sizeof pd; // 8 对应64位操作系统
    cout << ": size of *pd = " << sizeof(*pd) << endl; // 8 new double
    // cin.get();
    return 0;
}

代码解析图
在这里插入图片描述

指针和数组

// arraynew.cpp -- using the new operator for arrays
#include <iostream>
int main()
{
    using namespace std;
    double * p3 = new double [3]; // space for 3 doubles
    p3[0] = 0.2;                  // treat p3 like an array name
    p3[1] = 0.5;
    p3[2] = 0.8;
    cout << "p3[1] is " << p3[1] << ".\n"; // 0.5
    p3 = p3 + 1;                  // 0.5(指针前移)
    cout << "Now p3[0] is " << p3[0] << " and "; // 0.5
    cout << "p3[1] is " << p3[1] << ".\n"; // 0.8
    p3 = p3 - 1;                  // 0.2
    delete [] p3;                 // free the memory
    // cin.get();
    return 0;
}

在这里插入图片描述
在这里插入图片描述
地址一样,但是tell+1是+2,&tell+1是+20(电子版有错误)

// addpntrs.cpp -- pointer addition
#include <iostream>
int main()
{
    using namespace std;
    short tell[10] = {3, 2, 1};

    cout << tell << endl;
    cout << &tell << endl;
    cout << tell + 1 << endl;
    cout << &tell + 1 << endl;
    return 0;
}

结果

E:\xy\Code\cLionCode\cmake-build-debug\cLionCode.exe
0x7bd81ffc80
0x7bd81ffc80
0x7bd81ffc82
0x7bd81ffc94

进程已结束,退出代码为 0

指针和结构体

在这里插入图片描述
在这里插入图片描述

  • 5
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值