复习题
-
考虑下面的声明:
class RQ1{ private:char *st; // pointer to C-style string public:RQ1() { st = new char [1];strcpy(st, "");}RQ1(const char * s) {st = new char [strlen(s)+1];strcpy(st, s);}RQ1(const RQ1 & rq) {st = new char[strlen(rq.st)+1];strcpy(st, rq.st);}~RQ1() {delete [] st;}RQ & operator=(const RQ & rq);// more stuff };
将它转换为使用string 对象的声明。哪些方法不再需要显式定义?
答:拷贝构造,析构函数和赋值运算符重载不再需要显式定义。 -
在易于使用方面,指出 string 对象至少两个优于C-风格字符串的地方。
答:1. string 对象不需要手动分配内存空间和释放内存空间;2. string对象能够自动调整大小,且传参时,不需要一个额外的表示字符串大小的参数。 -
编写一个函数,用 string 对象作为参数,将 string 对象转换为全部大写。
答:void ToUpper(string & s) {for(int i = 0; i< s.size(); i++){s[i] = toupper(str[i]);} }
-
从概念或语法上说,下面哪个不是正确使用 auto_ptr 的方法(假设已经包含了所需的头文件)?
答:auto_ptr<int> pia(new int[20]); // wrong, use with new, not new[] auto_ptr<string> (new string); // wrong, no name for pointer int rigue = 7; auto_ptr<int>pr(&rigue); // wrong, memory not allocated by new auto_ptr dbl (new double); // wrong, omits <double>
-
如果生成一个存储高尔夫球棍(而不是数字)的栈,为何它(从概念上说)是一个坏的高尔夫袋子?
答:因为要取出旧的高尔夫球杆,需要将在它之后放入的所有球杆全部放入才行。 -
为什么说对于逐洞记录高尔夫成绩来说,set 容器是糟糕的选择?
答:因为set 容器要自动对成绩进行排序,不能保证原始的录入数据,而且set不能存储相同的数据。 -
既然指针是一个迭代器,为什么 STL 设计人员没有简单地使用指针来代替迭代器呢?
答:指针具有迭代器所需要的功能。但是对于不同数据容器(比如链表),使用迭代器才能使得接口能够统一按照类似于指针的处理方式来处理数据。 -
为什么 STL 设计人员仅定义了迭代器基类,而使用继承来派生其他迭代器类型的类,并根据这些迭代器来表示算法?
答:STL 方法使得可以将 STL 函数用于指向常规数组的常规指针以及指向 STL 容器类的迭代器,因此提高了通用性。
-
给出 vector 对象比常规数组方便的3个例子。
答: 可以将一个vector对象赋给另一个;vector管理自己的内存,因此可以将元素插入到vector中,并让它自动调整长度;使用 at() 方法,可以自动检查边界。 -
如果下面的程序是使用 list(而不是 vector)实现的,则该程序的哪些部分将是非法的?非法部分能够轻松修复吗?如果可以,如何修复呢?
// vect3.cpp -- using STL functions #include<iostream> #include<string> #include<vector> #include<algorithm>struct Review{std::string title;int rating; };bool operator<(const Review & r1, const Review & r2); bool worseThan(const Review & r1, const Review & r2);bool FillReview(Review & rr); void ShowReview(const Review & rr);int main(){using namespace std;vector<Review> books;Review temp;while (FillReview(temp)){books.push_back(temp);}if(books.size()>0){cout << "Thanke you. You entered the following "<< books.size() << " ratings:\n"<< "Rating\tBook\n";for_each(books.begin(), books.end(), ShowReview);sort(books.begin(), books.end());cout << "Sorted by title:\nRating\tBook\n";for_each(books.begin(),books.end(),ShowReview);sort(books.begin(), books.end(), worseThan);cout << "Sorted by rating:\nRating\tBook\n";for_each(books.begin(), books.end(), ShowReview);random_shuffle(books.begin(), books.end());cout << "After shuffling:\nRating\tBook\n";for_each(books.begin(), books.end(), ShowReview);}else{cout << "No entries. ";}cout << "Bye.\n";return 0; }bool operator<(const Review & r1, const Review & r2){if (r1.title < r2.title){return true;}else if (r1.title == r2.title && r1.rating < r2.rating){return true;}else{return false;} }bool worseThan(const Review & r1, const Review & r2){if (r1.rating < r2.rating){return true;}else{return false;} }bool FillReview(Review & rr){std::cout << "Enter book title (quit to quit): ";std::getline(std::cin, rr.title);if(rr.title=="quit"){return false;}std::cout << "Enter book rating: ";std::cin >> rr.rating;if (!std::cin){return false;}// get rid of rest of input linewhile(std::cin.get() != '\n'){continue;}return true; }void ShowReview(const Review & rr){std::cout << rr.rating << "\t" << rr.title << std::endl; }
答:sort函数非法,可以改成使用list的成员sort()方法;
random_shuffle()非法,因为list不支持随机访问迭代器,可以先将list的内容复制到一个vector中,打乱后在复制回list。 -
假设有如下程序所示的函数符 TooBig,下面的带啊吗有何功能?赋给 bo 的是什么值?
bool bo = TooBig<int>(10)(15);
// functor.cpp -- using a functor #include <iostream> #include <list> #include <iterator> #include <algorithm>template<class T> // functor class defines operator()() class TooBig{ private:T cutoff; public:TooBig(const T & t) : cutoff(t) {}bool operator()(const T & v) { return v > cutoff; } };void outint(int n) { std::cout << n << " ";}int main(){using std::list;using std::cout;using std::endl;TooBig<int> f100(100); // limit = 100int vals[10] = {50, 100, 90, 180, 60, 210, 415, 88, 188, 201};list<int> yadayada(vals, vals+10); // range constructorlist<int> etcetera(vals, vals+10);// C++11 cna use the following instead// list<int> yadayada = {50, 100, 90, 180, 60, 210, 415, 88, 188, 201};// list<int> etcetera {50, 100, 90, 180, 60, 210, 415, 88, 188, 201};cout << "Original lists:\n";for_each(yadayada.begin(), yadayada.end(), outint);cout << endl;std::for_each(etcetera.begin(), etcetera.end(), outint);cout << endl;yadayada.remove_if(f100);etcetera.remove_if( TooBig<int> (200));cout << "Trimmed lists:\n";for_each(yadayada.begin(), yadayada.end(), outint);cout << endl;for_each(etcetera.begin(), etcetera.end(), outint);cout << endl;return 0; }
答:该代码将 15 和 10 比较大小,返回 15 > 10? 结果为 true。