《C++ Primer Plus》第十六章学习笔记
这一章粗略介绍了一下STL和泛型编程,我也是粗略地看了一看。
181:String类构造函数:
182:关于输入:
C风格字符串有3种方式:
char info[100]; cin >> info; // read a word cin.getline(info, 100); // read a line, discard \n cin.get(info, 100); // read a line, leave \n in queue
string类对象,有2种方式:
string stuff; cin >> stuff; // read a word getline(cin, stuff); // read a line, discard \n
两个版本的getline()都有一个可选参数,用于指定使用哪个字符来确定输入的边界。
cin.getline(info,100,’:’); // read up to :, discard : getline(cin,stuff, ‘:’); // read up to :, discard :
183:很多C库都提供了stricmp()或_stricmp()函数,能执行不区分大小写的比较。
184:关于string类的详细用法见:http://www.cplusplus.com/reference/string/string/
185:auto_ptr的思想:回收栈中指针变量占用的内存的同时,也删除该指针指向的动态内存,不用显式delete
186:只能通过new分配的内存使用auto_ptr对象,而不要对由new[]分配的或通过声明变量分配的内存(栈区)使用它。
187:Institute the concept of ownership, with only one smart pointer allowed to own a particular object. Only if the smart pointer owns the object will its constructor delete the object. Then have assignment transfer ownership. This is the strategy used for auto_ptr.
188:The STL provides a collection of templates representing containers, iterators, function objects, and algorithms. The STL is not an example of object-oriented programming. Instead, it represents a different programming paradigm called generic programming.
Object-oriented programming concentrates on the data aspect of programming, whereas generic programming concentrates on algorithms. The main things the two approaches have in common are abstraction and the creation of reusable code, but the philosophies are quite different.
189:Why Iterators? Just as templates make algorithms independent of the type of data stored, iterators make the algorithms independent of the type of container used.
190:STL定义了5种迭代器,分别是输入迭代器(input iterator)、输出迭代器(output iterator)、正向迭代器(forward iterator)、双向迭代器(bidirectional iterator)、随机访问迭代器(random access iterator)。为何需要这么多迭代器?目的是为了在编写算法时尽可能使用要求最低的迭代器,并让它适用于容器的最大区间。
191:主要迭代器的功能:
192:The 11 container types are deque, list, queue, priority_queue, stack, vector, map, multimap, set, multiset, and bitset.
书中的错误:
P589 表格最后一行的描述中的”end]”应改为”end)”;
P591 倒数第18行漏了个”cin”;
P593 表格中第3个find()第二个参数应该没有默认值;
P594 第7行”last_first_of”应改为”find_last_of”;
P601倒数第6行的“构造函数”是不是应改成“析构函数”;
P603第一行漏了两个尖括号;P605 第17、19行的错误和P589一样;
P617 第11行的”[10]“应改为”(10)”;