我的Github地址:https://github.com/lanbeilyj/Accerlerated-C-plus-plus
最后章节综合度较大,一些相关习题也综合在一起了。
12-0. Compile, execute, and test the programs in this chapter.
Ans:见Github。
12-1. Reimplement the Str
class, but choose an implementation strategy that requires that the class manage the storage itself. For example, you might store an array ofchar
and a length. Consider what implications this change in design has for copy control. Also consider the cost of usingVec
, (e.g., in storage overhead).
Ans:见Github。
12-2. Implement the c_str
, data
, and copy functions.
Ans:见Github。
12-3. Define the relational operators for Str
. In doing so, you will want to know that the<cstring>
header defines a function calledstrcmp
, which compares two character pointers. The function returns a negative integer if the null-terminated character array denoted by the first pointer is less than the second, zero if the two strings are equal, or a positive value if the first string is greater than the second.
Ans:见Github。
12-4. Define the equality and inequality operators for Str
.
Ans:见Github。
12-5. Implement concatenation for Str
so as not to rely on conversions fromconst char*
.
Ans:见Github。
12-6. Give Str
an operation that will let us implicitly use aStr
object as a condition. The test should fail if theStr
is empty, and should succeed otherwise.
Ans:见Github。
12-7. The standard string
class provides random-access iterators to manipulate thestring
's characters. Add iterators and the iterator operations begin and end to yourStr
class.
Ans:见Github。
12-8. Add the getline
function to the Str
class.
Ans:见Github。
12-9. Use class ostream_iterator
to reimplement theStr
output operator. Why didn't we ask you to reimplement the input operator using classistream_iterator
?
Ans:见Github。
12-10. Having seen in §12.1/212 how Str
defined a constructor that takes a pair of iterators, we can imagine that such a constructor would be useful in classVec
. Add this constructor toVec
, and reimplement Str
to use the Vec
constructor instead of callingcopy
.
Ans:见Github。
12-11. If you add the operations listed in these exercises, then you can use thisStr
class in all the examples in this book. Reimplement the operations on character pictures from Chapter 5 and thesplit
functions from §5.6/87 and §6.1.1/103.
Ans:见Github。
12-12. Define the insert
function that takes two iterators for theVec
andStr
classes.
Ans:见Github。
12-13. Provide an assign
function that could be used to assign the values in an array to aVec
.
Ans:见Github。
12-14. Write a program to initialize a Vec
from astring
.
Ans:见Github。
12-15. The read_hw
function from §4.1.3/57 checked the stream from which it read to determine whether the function had hit end-of-file, or had encountered an invalid input. OurStr
input operator does no such check. Why? Will it leave the stream in an invalid state?
Ans:4.1.3中因为需要从流中读取每个学生的所有家庭作业成绩,因而需要判断,当一个学生的所有homework输入完成后流会变成无效状态,此时需要清除流状态以使流对下一个学生有效;若不判断则则只能对一个学生的成绩进行处理。在本章的Str中没有判断,是我们从流中逐个读取一个字符并判断,流会处于无效状态。
std::istream& operator>> (std::istream& is,Str& s) { s.data.clear(); char c; while(is.get(c) && isspace(c)); if(is) { do { s.data.push_back(c); }while(is.get(c) && !isspace(c)); if(is) is.unget(); } return is; }
4.1.3
istream& read_hw(istream& is,vector<double>& hw) { if(is) { hw.clear(); double x; while(is>>x) hw.push_back(x); is.clear(); } return is; }