C++ Primer问题总结(2)

1.第3章

1.要学会使用直接初始化

string s1("hiya"); 

以前都是用的拷贝初始化

string s2 = string(10,'c');

这非常不理想,可读性较差,也不简洁
2.用getline读取一整行的时候是将换行符读进了流中的,但是不会读入要写入的对象或者文件中的,这是因为触发getline函数返回的那个换行符实际上被丢弃了。
3.字面值不能直接相加
比如:

string s3 = "hello" + "," + s2;	//error

至少保证+的两侧的运算对象至少有一个是stirng类对象

string s3 = "hello"  + s2 + ",";	//true

这是因为运用的是operator + 重载技术,+的两端必须要有一个对象才能调用运算符重载函数。
4.初始化如果使用的是花括号,可以表述为列表初始化,初始化过程会尽可能地把花括号的值当成是元素初始值的列表来处理。
5.另外如果初始化时使用了花括号的形式但是提供的值又不能用来列表初始化,就要考虑用这样的值来构造对象,比如:

vector<string> v5{"hi"};	//列表初始化
vector<string> v6{10,"hi"};	//创建v6为vector<string>类对象

因此:确认无法执行列表初始化后,编译器会尝试用默认值初始化vector对象。
6.为STL标注库类添加元素可以使用push_back(),访问元素可以使用for(auto i : vname).
7.vector对象(以及string对象)的下标运算符可用于访问已存在的元素,而不能用于添加元素。
8.用decltype关键字转换数组名的时候,并不会将类型设置为指针(指向数组首元素),而是返回一个数组
比如:

int ia[] = {0,1,2,3,4,5};
decltype(ia) ia3 = {0,1,2,3,4,5};

在这里ia不是指向数组第一个元素,而是表示数组。
9.cstring库中函数处理字符串字面值结构的时候,传入函数的参数是数组名,实际上传入的是整个数组而非指向数组首元素地址的指针。
第3章练习3.43

#include <iostream>

using std::cout;
using std::endl;

int main()
{
    int ia[3][4] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11};

    // a range for to manage the iteration
    for (const int(&p)[4] : ia)
        for (int q : p) cout << q << " ";
    cout << endl;

    // ordinary for loop using subscripts
    for (size_t i = 0; i != 3; ++i)
        for (size_t j = 0; j != 4; ++j) cout << ia[i][j] << " ";
    cout << endl;

    // using pointers.
    for (int(*p)[4] = ia; p != ia + 3; ++p)
        for (int* q = *p; q != *p + 4; ++q) cout << *q << " ";
    cout << endl;

    return 0;
}

为什么第一个p要加引用,这是为了防止编译器自动将p转换为指针。
第三章 3.44

#include <iostream>

using std::cout;
using std::endl;

int main()
{
    int ia[3][4] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11};

    // a range for to manage the iteration
    // use type alias
    using int_array = int[4];
    for (int_array& p : ia)
        for (int q : p) cout << q << " ";
    cout << endl;

    // ordinary for loop using subscripts
    for (size_t i = 0; i != 3; ++i)
        for (size_t j = 0; j != 4; ++j) cout << ia[i][j] << " ";
    cout << endl;

    // using pointers.
    // use type alias
    for (int_array* p = ia; p != ia + 3; ++p)
        for (int* q = *p; q != *p + 4; ++q) cout << *q << " ";
    cout << endl;

    return 0;
}

第三章3.45

#include <iostream>

using std::cout;
using std::endl;

int main()
{
    int ia[3][4] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11};

    // a range for to manage the iteration
    for (auto& p : ia)
        for (int q : p) cout << q << " ";
    cout << endl;

    // ordinary for loop using subscripts
    for (size_t i = 0; i != 3; ++i)
        for (size_t j = 0; j != 4; ++j) cout << ia[i][j] << " ";
    cout << endl;

    // using pointers.
    for (auto p = ia; p != ia + 3; ++p)
        for (int* q = *p; q != *p + 4; ++q) cout << *q << " ";
    cout << endl;

    return 0;
}

第1个实现和第3个实现的p一个是引用 一个是指针。引用p表示一个数组,而指针p表示数组的首地址。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值