C++ Primer 12章习题

12.1 b1有4个元素,~~b2已销毁~~b2也有4个元素??
12.2

class StrBlob
{
public:
    typedef vector<string>::size_type size_type;
    StrBlob();
    StrBlob(initializer_list<string> il);
    size_type size() const { return data->size(); }
    bool empty() const{ return data->empty(); }
    void push_back(const string &t) { data->push_back(t); }
    void pop_back();
    string &front();
    const string & front() const;
    string &back();
    const string & back() const;
private:
    shared_ptr<vector<string>> data;
    void check(size_type i, const 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() 
{ 
    check(0, "front on empty StrBlob"); 
    return data->front(); 
}
const 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 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();
}

12.3 不需要,因为会改变data
12.4 size_type没有小于0的
12.5 允许隐式转换。
12.6

#include "stupidsource.h"
#include <iostream>
using namespace std;

vector<int> *first()
{
    auto p = new vector<int>();
    return p;
}

vector<int>* second(istream &in, vector<int> * vec)
{
    int temp;
    while (cin >> temp)
        vec->push_back(temp);
    return vec;
}

void third(vector<int> * vec)
{
    for (const auto c : *vec)
        cout << c << ends;
    cout << endl;
    delete vec;
    vec=nullptr
}
int main()
{
    third(second(cin, first()));
    return 0;
}

12.7

#include <iostream>
#include <fstream>
#include <sstream>
#include <vector>
#include <string>
#include <memory>

using namespace std;

shared_ptr<vector<int>> first()
{
    return make_shared<vector<int>>();
}

void second(istream &in,shared_ptr<vector<int>> p)
{
    int temp;
    if (p)
        while (in >> temp)
            p->push_back(temp);
}

void third(ostream &out, shared_ptr<vector<int>> p)
{
    for (const auto c : *p)
        cout << c << ends;
    cout << endl;
}

int main(int argc , char* argv[])
{
    auto p = first();
    second(cin, p);
    third(cout, p);
    return 0;
}

12.8 new失败会抛出异常。改为new (throw)
12.9 略
12.10 可以。
12.11 process函数结束时提前销毁一次内存。
12.12 (a)合法(b)不合法,不能隐式转换合法
(c)不合法,不能隐式转换(d)不合法,提前销毁内存。
12.13 销毁p指向的内存,sp成为空悬指针。
12.14-12.15略
12.16 略
12.17 (a)错误
(b)错误,临时变量不在堆中。
(c)正确
(d)错误,同b
(e)正确
(g)错误,没有get成员
12.18 shared_ptr不是一个指针单独占用一块内存
12.19-12.22略
12.23

#include "stdafx.h"
#include <iostream>
#include<cstring>
#include<string>
using namespace std;

int main(int argc, char* argv[])
{
    const char *chartemp1 = "stupidfengyuhang";
    const char *chartemp2 = "stupidfengyuhang";
    char *ptr1 = new char[strlen(chartemp1) + strlen(chartemp2) + 1];
    strcpy(ptr1, chartemp1);
    strcat(ptr1, chartemp2);
    cout << ptr1 << endl;
    delete[] ptr1;

    string strtemp1("stupidstupid"), strtemp2("shabishabi");
    string *ptr2 = new string(strtemp1 + strtemp2);
    cout << *ptr2 << endl;
    delete ptr2;
}

12.24 输入的字符串长度不能超过分配的数组长度-1
12.25 delete[] p;

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值