Accelerated C++:通过示例进行编程实践——练习解答(第11章)

我的Github地址:https://github.com/lanbeilyj/Accerlerated-C-plus-plus

11-0. Compile, execute, and test the programs in this chapter.

Ans:见Github。

11-1. The Student_info structure that we defined in Chapter 9 did not define a copy constructor, assignment operator, or destructor. Why not?

Ans:因为在编写类时,如果没有显示地定义复制构造函数、赋值运算符、析构函数,那么编译器就会自动为类生成相应的默认版本函数,从而进行一些默认的操作。对Student_info类的数据成员复制,该类会利用string和vector类的复制构造函数来分别复制name和homework成员;对另外两个double类型的变量成员midterm和fin则进行直接的复制。

11-2. That structure did define a default constructor. Why?

Ans:只有没有定义任何构造函数编译器才会自动生成一个不带任何参数的构造函数,而如果定义了任何一个构造函数,那么编译器就不会自动生成默认的构造函数。

11-3. What does the synthesized assignment operator for Student_info objects do?

Ans:赋值是先在定义一个变量并初始化后,调用了“=”才被调用;因此赋值会先删除原内存单元的值,然后在赋予其新的值。Student_info对象在调用自动生成的操作函数具体的操作有:自动调用string和vector的复制构造函数对name和homework进行赋值;直接对两个double变量midterm和fin进行赋值;

11-4. How many members does the synthesized Student_info destructor destroy?

Ans:见Github。

11-5. Instrument the Student_info class to count how often objects are created, copied, assigned, and destroyed. Use this instrumented class to execute the student record programs from Chapter 6. Using the instrumentedStudent_info class will let you see how many copies the library algorithms are doing. Comparing the number of copies will let you estimate what proportion of the cost differences we saw are accounted for by the use of each library class. Do this instrumentation and analysis.

Ans:具体见Github。

struct Student_info
{
    std::string name;
    double midterm,fin;
    std::vector<double> homework;

    Student_info()
    {
        std::cout<<"construct"<<std::endl;
    }
    Student_info(const Student_info& s)
    {
        copy(s);
        std::cout<<"copy"<<std::endl;
    }
    void copy(const Student_info& s)
    {
        name=s.name;
        midterm=s.midterm;
        fin=s.fin;
        homework=s.homework;
    }
    Student_info& operator=(const Student_info& s)
    {
        std::cout<<"assign"<<std::endl;
        if(&s!=this)
            copy(s);
        return *this;
    }
    ~Student_info()
    {
        std::cout<<"del"<<std::endl;
    }
};

11-6. Add an operation to remove an element from a Vec and another to empty the entireVec. These should behave analogously to theerase andclear operations onvectors.

Ans:具体见Github。

void clear()
{
        uncreate();
}

iterator erase(iterator it)
{
        for(iterator i=it;i!=avail;++i)
        {
            alloc.destroy(i);
            if ((i + 1) != avail) alloc.construct(i, *(i + 1));
        }
        --avail;
        return it;
}

11-7. Once you've added erase and clear toVec, you can use that class instead ofvector in most of the earlier programs in this book. Rewrite theStudent_info programs from Chapter 9 and the programs that work with character pictures from Chapter 5 to useVecs instead ofvectors.

Ans:因为需要判断学生是否做了家庭作业,故需要在11-6基础上添加判断容器vector<double> homework是否为空的empty()成员函数,具体见Github。

bool empty() const { return data == avail; }

11-8. Write a simplified version of the standard list class and its associated iterator.

Ans:
vector:利用数组实现;顺序存储结构,采用预分配策略分配内存;可随机访问元素所以利用索引实现元素访问;仅支持后插故只实现push_back();

list:利用双向链表实现;采用链式存储结构,不需要预分配;不支持随机访问即不支持索引,利用迭代器++和--以实现遍历;支持前插push_fron()和后插push_back()以及insert()等操作;

//节点结构如下:
template <class T>
struct node{
    T data;//数据域
    node<T> *prior,*next;//前向和后向指针

    node(){}
    node(const T& t)
    {
        data=t;
        next=0;
        prior=0;
    }
};

具体见Github。

11-9. The grow function in §11.5.1/208 doubles the amount of memory each time it needs more. Estimate the efficiency gains of this strategy. Once you've predicted how much of a difference it makes, change thegrow function appropriately and measure the difference.

Ans:见Github。

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值