当前位置: 首页 > news >正文

C++ Primer 课后习题详解 | 12.1.1 shared_ptr 类

🎈 作者:Linux猿

🎈 简介:CSDN博客专家🏆,华为云享专家🏆,Linux、C/C++、云计算、物联网、面试、刷题、算法尽管咨询我,关注我,有问题私聊!

🎈 关注专栏: C/C++面试通关【精讲】 优质好文持续更新中……🚀🚀🚀

🎈 欢迎小伙伴们点赞👍、收藏⭐、留言💬


本篇文章是对 C++ Primer 第五版 第 12.1.1 节课后习题 12.1 ~ 12.5 的讲解。因为习题中涉及类 StrBlob,所以先将 StrBlob 类代码展示如下。

#include <iostream>
#include <vector>
#include <string>
#include <memory>
using namespace std;

class StrBlob {
public:
    typedef std::vector<std::string>::size_type size_type;
    StrBlob();
    StrBlob(std::initializer_list<std::string> il);
    size_type size() const { return data->empty(); }
    bool empty() const { return data->empty(); }
    // 添加和删除元素
    void push_back(const std::string &t) { data->push_back(t); }
    void pop_back();
    // 元素访问
    std::string& front();
    std::string& back();
private:
    std::shared_ptr<std::vector<std::string>> data;
    // 如果 data[i] 不合法,抛出一个异常
    void check(size_type i, const std::string &msg) const;
};

StrBlob::StrBlob(): data(make_shared<vector<string>>()) {}

StrBlob::StrBlob(initializer_list<string> il):
    data(make_shared<vector<string>>(il)) {}

void StrBlob::check(size_type i, const string &msg) const
{
    if (i >= data->size()) {
        throw out_of_range(msg);
    }
}

string& StrBlob::front()
{
    // 如果 vector 为空,check 会抛出一个异常
    check(0, "front on empty StrBlob");
    return data->front();
}

string& StrBlob::back()
{
    check(0, "back on empty StrBlob");
    return data->back();
}

void StrBlob::pop_back()
{
    check(0, "pop_back on empty StrBlob");
    data->pop_back();
}


int main()
{
    StrBlob b1;
    {
        StrBlob b2 = {"a", "an", "the"};
        b1 = b2;
        b2.push_back("about");
    }
    return 0;
}

一、练习 12.1

1.1 题目要求

在此代码的结尾,b1 和 b2 各包含多少个元素?

StrBlob b1;
{
    StrBlob b2 = {"a", "an", "the"};
    b1 = b2;
    b2.push_back("about");
}

1.2 题目解析

在上述代码中,b2 赋值给 b1,因为 StrBlob 类中的 data 是 shared_ptr,所以 b1 和 b2 指向同一个内存空间。但是,b2 是一个局部变量,程序跳出左括号( ‘}’ ) 后 b2 会被销毁。但是,里面的元素并没有被销毁,只是指向该内存空间的引用计数减一。

所以,b1 和 b2 各包含 4 个元素。

二、练习 12.2

2.1 题目要求

编写你自己的 StrBlob 类,包含 const  版本的 front 和 back。

2.2 题目解析

添加 const 版本的 font 和 back 代码如下所示。

#include <iostream>
#include <vector>
#include <string>
#include <memory>
using namespace std;

class StrBlob {
public:
    typedef std::vector<std::string>::size_type size_type;
    StrBlob();
    StrBlob(std::initializer_list<std::string> il);
    size_type size() const { return data->empty(); }
    bool empty() const { return data->empty(); }
    // 添加和删除元素
    void push_back(const std::string &t) { data->push_back(t); }
    void pop_back();
    // 元素访问
    std::string& front();
    const std::string &front() const; // 增加 const front !!!

    std::string& back();
    const std::string &back() const; // 增加 const back !!!
private:
    std::shared_ptr<std::vector<std::string>> data;
    // 如果 data[i] 不合法,抛出一个异常
    void check(size_type i, const std::string &msg) const;
};

StrBlob::StrBlob(): data(make_shared<vector<string>>()) {}

StrBlob::StrBlob(initializer_list<string> il):
    data(make_shared<vector<string>>(il)) {}

void StrBlob::check(size_type i, const string &msg) const
{
    if (i >= data->size()) {
        throw out_of_range(msg);
    }
}

string& StrBlob::front()
{
    // 如果 vector 为空,check 会抛出一个异常
    check(0, "front on empty StrBlob");
    return data->front();
}

// 新增 const front
const std::string &StrBlob::front() const {
  check(0, "front on empty StrBlob");
  return data->front();
}

string& StrBlob::back()
{
    check(0, "back on empty StrBlob");
    return data->back();
}

// 新增 const back
const std::string &StrBlob::back() const
{
  check(0, "back on empty StrBlob");
  return data->back();
}

void StrBlob::pop_back()
{
    check(0, "pop_back on empty StrBlob");
    data->pop_back();
}

// test
void testStrBlob(StrBlob &sb)
{
    std::cout<<"function void testStrBlob(StrBlob &sb)"<<std::endl;
    try {
        sb.push_back("abc");
        sb.push_back("def");
        std::cout << "front: " << sb.front() << " back: " << sb.back() << std::endl;
        sb.pop_back();
        std::cout << "front: " << sb.front() << " back: " << sb.back() << std::endl;
        sb.pop_back();
        std::cout << "front: " << sb.front() << " back: " << sb.back() << std::endl;
    } catch (std::out_of_range err) {
        std::cerr << err.what() << " out of range" << std::endl;
    } catch (std::exception err) {
        std::cerr << err.what() << std::endl;
    }
    std::cout << std::endl;
}

// test const
void testStrBlob(const StrBlob &sb)
{
    std::cout<<"void testStrBlob(const StrBlob &sb)"<<std::endl;
    try {
        std::cout << "front: " << sb.front() << " back: " << sb.back() << std::endl;
    } catch (std::out_of_range err) {
        std::cerr << err.what() << " out of range" << std::endl;
    } catch (std::exception err) {
        std::cerr << err.what() << std::endl;
    }
    std::cout << std::endl;
}

void test12_2()
{
    // 非常量测试
    std::cout<<"非常量测试"<<std::endl;
    StrBlob sb1;
    StrBlob sb2{"Hello", "World"};
    StrBlob sb3 = {"Bye", "World"};

    testStrBlob(sb1);
    testStrBlob(sb2);
    testStrBlob(sb3);

    // 常量测试
    std::cout<<"常量测试"<<std::endl;
    const StrBlob csb1;
    const StrBlob csb2{"This", "Blob"};

    testStrBlob(csb1);
    testStrBlob(csb2);
    testStrBlob({"ppp", "qqq"});
}

int main()
{
    test12_2();
    return 0;
}

 在上述代码中,需要注意的是,在声明和定义处都要添加 const 修饰,const 修饰的成员函数是可以构成重载的,上述代码中也进行了测试。

三、练习 12.3

3.1 题目描述

StrBlob 需要 const 版本的 push_back 和 pop_back 吗?如果需要添加进去。否则,解释为什么不需要。

3.2 题目解析

首先,需要理解 const 修饰的成员函数不能修改成员变量。但是,本题中 push_back 和 pop_back 函数中改变的并不是 data 的值,而是对 data 指向的内容进行修改,所以,函数 push_back 和 pop_back 使用 const 修饰也是可以的。但是,这里不建议添加 const 修饰,因为添加上 const 有误导性,因为函数名有修改的意思,但 const 有不能修改的意思,逻辑上有矛盾!

四、练习 12.4

4.1 题目描述

在我们的 check 函数中,没有检查 i 是否大于 0。为什么可以忽略这个检查?

4.2 题目解析

因为 check 函数第一个参数的类型是 size_type,size_type 是无符号整型(unsigned),因此 size_type 会保证第一个参数一定是一个大于等于 0 的整数,即使传入一个负数,例如:

#include <iostream>
#include <vector>
using namespace std;

typedef std::vector<std::string>::size_type size_type;
void check(size_type i)
{
    std::cout<<"i = "<<i<<std::endl;
}

int main()
{
    check(-1);
}

上述代码输出为:

i = 18446744073709551615

五、练习 12.5

5.1 题目描述

我们未编写接受一个 initializer_list explicit 参数的构造函数。讨论这个设计策略的优点和缺点。

5.2 题目解析

本题首先需要理解 explicit 的作用,explicit 用于防止构造函数进行隐式转换,如果添加 explicit,将阻止从 initializer_list 到 StrBlob 的转换。那么,优点和缺点就很明显了。

优点:

(1)可以使用赋值将初始化器列表分配给 StrBlob 对象;

(2)可以将初始值设定项列表传递给需要 StrBlob 参数的函数,说白了就是从 initializer_list 到 StrBlob 的转换。

缺点:

(1)有时我们可能会忽略编译器进行的隐式转换,这可能会导致错误。


🎈 感觉有帮助记得「一键三连支持下哦!有问题可在评论区留言💬,感谢大家的一路支持!🤞猿哥将持续输出「优质文章回馈大家!🤞🌹🌹🌹🌹🌹🌹🤞


相关文章:

  • OPTIONS 漏洞修复
  • 卷积神经网络 CNN 基础概念
  • 2022年工作总结
  • 转行做程序员,哪种编程语言既高薪又适合你?
  • Python使用pandas导入csv文件内容
  • 【TypeScript】TS安装与使用
  • 河道水文标尺监测系统 OpenCv
  • 通用预约小程序,可广泛应用于医疗、政务、教育、培训、体育、金融、生活服务等行业领域,基于腾讯小程序云开发,无须服务器和域名
  • maven中的pom
  • 基于形状的匹配提纲(第六个问题解决)
  • 2022年转行编程选哪门语言?这份报告给你答案!
  • Jetpack Compose中的导航路由
  • Java项目:springboot课程自动排课系统
  • 启动项目端口被占用无奈只能重启?程序员的电脑绝不允许重启!
  • Huffman二进制编码以及文本的压缩与解压
  • 单商户商城系统功能拆解51—应用中心—评价助手
  • 基于注解方式实现Spring Security忽略拦截
  • Leetcode 1792. 最大平均通过率 题解
  • k8s之多方面多维度的资源隔离和限制(namespace,LimitRange,ResourceQuota)
  • Himall商城更新插件列表\获取插件程序集文件\ 深复制IPlugin