C++Primer 第9章-顺序容器-课后习题答案 下载本文

内容发布更新时间 : 2024/5/26 0:52:30星期一 下面是文章的全部内容请认真阅读。

}

// show

cout << \ After erase, the elements of iVec are:\ << endl; for ( vector::iterator it = iVec.begin(); it != iVec.end(); ++it ) cout << *it << \;

cout << \ After erase, the elements of iLst are:\ << endl; for ( list::iterator it = iLst.begin(); it != iLst.end(); ++it ) cout << *it << \; cout << endl; system(\); return 0;

27.编写程序处理一个string类型的list容器。在该容器中寻找一个特殊值,如果找到,则将它删除掉。用deque容器重写上述程序。

// 11.15_9.27_StringList_Find_Earse.cpp : 定义控制台应用程序的入口点。 //

#include \#include #include #include #include using namespace std;

int _tmain(int argc, _TCHAR* argv[]) { string strSerVal(\); string strArr[] = { \, \, \ }; list strLst( strArr, strArr + 3 ); list::iterator Lit = find( strLst.begin(), strLst.end(), strSerVal ); if ( Lit != strLst.end() ) {

}

cout << \ << endl; strLst.erase( Lit ); }

system(\); return 0;

用deque重写以上程序: string strSerVal(\); string strArr[] = { \, \, \ }; deque strDeq( strArr, strArr + 3 ); deque::iterator Dit = find( strDeq.begin(), strDeq.end(), strSerVal ); if ( Dit != strDeq.end() ) { cout << \ << endl; strDeq.erase( Dit ); }

28.编写一个程序将一个list容器的所有元素赋值给一个vector容器,其中list容器中存储的是指向C风格字符串的char*指针,而vector容器的元素则是string类型。 // 11.15_9.28_assignListToVector.cpp : 定义控制台应用程序的入口点。 //

#include \#include #include #include #include using namespace std; int _tmain(int argc, _TCHAR* argv[]) {

}

char *c_arr[] = { \, \, \ }; list pLst;

pLst.assign( c_arr, c_arr+3 );

cout << \; for ( list::iterator it = pLst.begin(); it != pLst.end(); ++it ) { cout << *it << \; }

vector strVec;

strVec.assign( pLst.begin(), pLst.end() );

cout << \; for ( vector::iterator it = strVec.begin(); it != strVec.end(); ++it ) { cout << *it << \; }

cout << endl; system(\); return 0;

29.解释vector的容量和长度之间的区别。为什么在连续存储元素的容器中需要支持“容量”的概念? 而非连续的容器,如list,则不需要。

容量capacity,是指容器在必须分配新的存储空间之前可以存储的元素的总数。而长度是指容器当前拥有的元素的个数。

对于连续存储的容器来说,容器中的元素时连续存储的,当往容器中添加一个元素时,如果容器中已经没有空间容纳新的元素,则为了保持元素的连续存储,必须重新分配存储空间,用来存放原来的元素以及新添加的元素:首先将存放在旧存储空间中的元素复制到新的存储空间里,然后插入新的元素,最后撤销旧的存储空间。如果在每次添加新元素时,都要这样分配和撤销内存空间,其性能将会慢得让人无法接受。为了提高性能,连续存储元素的容器实际分配的容量要比当前所需的空间多一些,预留了一些额外的存储区,用于存放新添加的元素,使得不必为每个新的元素重新分配容器。所以,在连续存储元素的容器中需要支持“容量”的概念。

而对于不连续存储的容器,不存在这样的内存分配问题。例如当在list容器中添加一个元素,标准库只需创建一个新元素,然后将该新元素连接到已经存在的链表中,不需要重新分配存储空间,也不必复制任何已存在的元素。所以这类容器不需要支持“容量”的概念。

30.编写程序研究标准库为vector对象提供的内存分配策略。

// 11.15_9.30_vector_capacity_and_reserve.cpp : 定义控制台应用程序的入口点。 //

#include \#include #include using namespace std;

int _tmain(int argc, _TCHAR* argv[]) { vector ivec; cout << \ << ivec.size() << endl << \ << ivec.capacity() << endl; // add new elements: for ( size_t ix = 1; ix != 11; ++ix ) { ivec.push_back( ix ); cout << \ << ivec.size() << endl << \ << ivec.capacity() << endl; } // use up the current capacity of the vector while ( ivec.size() != ivec.capacity() ) { ivec.push_back( 0 ); } // add one more ivec.push_back(0); // show the current capacity of the vector cout << \ << ivec.size() << endl << \ << ivec.capacity() << endl; // resize the capacity of the vector ivec.reserve( 100 ); // show the current capacity of the vector cout << \ << ivec.size() << endl << \ << ivec.capacity() << endl; // use up the current capacity of the vector while ( ivec.size() != ivec.capacity() )