我的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 onvector
s.
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 useVec
s instead ofvector
s.
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。