作者:
Scott Meyers 出版社: Addison-Wesley Professional 副标题: 50 Specific Ways to Improve Your Use of the Standard Template Library 译者:
-
/
- 出版年: 2001-6-16 页数: 288 定价: USD 54.99 装帧: Paperback 丛书:Addison-Wesley Professional Computing Series ISBN: 9780201749625
To avoid resource leaks
when you have containers of pointers that should be deleted, you must either replace
the pointers with smart reference-counting pointer objects (such as Boost's shared_ptr)
or you must manually delete each pointer in the container before the container is
destroyed. (查看原文)
Addison-Wesley Professional Computing Series (共46册),
这套丛书还有
《Effective Tcl/Tk Programming》,《Programming with POSIX® Threads》,《The Go Programming Language》,《Generic Programming and the STL》,《Large-Scale C++: Volume I》 等。
好书,深入浅出,学习STL必读 查漏补缺,用STL还不熟练,加油!加油!这是一本好书,配合STL的源码一起阅读。我最近还是多看看别人写的代码,学习别人的思维模式。ps, 这本书适合对STL理解深入,《C++ Primer Plus》还是没有仔细讨论STL。pps,看的英文版本的,中文版本字都重叠起来了lol
重点: item 1, 5, 9, 14, 18, 24, 28, 31 Item 5. Prefer range member functions to their single-element counterparts. Item 9. eg: container.erase(remove(c.begin(), c.end(), target), c.end()); (1) To eliminate all objects in a container that have a particular ...
(展开)
• The standard STL sequence containers, vector, string, deque, and list. • The standard STL associative containers, set, multiset, map and multimap. • The nonstandard sequence containers slist and rope. slist is a singly linked list, and rope is essentially a heavy-duty string. • The nonstandard associative containers hash_set, hash_multiset, hash_map, and hash_mul...
2020-06-19 23:30:04
• The standard STL sequence containers, vector, string, deque, and list.
• The standard STL associative containers, set, multiset, map and multimap.
• The nonstandard sequence containers slist and rope. slist is a singly linked list, and rope is essentially a heavy-duty string.
• The nonstandard associative containers hash_set, hash_multiset, hash_map, and hash_multimap.
Contiguous-memory containers (also known as array-based containers] store their elements in one or more (dynamically allocated) chunks of memory, each chunk holding more than one container element. If a new element is inserted or an existing element is erased, other elements in the same memory chunk have to be shifted up ordown to make room for the new element or to fill the space formerly occupied by theerased element. This kind of movement affects both performance (see Items 5 and 14) and exception safety (as we'll soon see). The standard contiguous-memory containers are vector, string, and deque. The nonstandard rope is also a contiguous-memory container.
Node-based containers store only a single element per chunk of (dynamically allocated) memory. Insertion or erasure of a container element affects only pointers to nodes, not the contents of the nodes themselves, so element values need not be moved when something is inserted or erased. Containers representing linked lists, such as list and slist, are node-based, as are all the standard associative containers. typically implemented as balanced trees.)
Item 1: Choose your containers with care. Item 2: Beware the illusion of container-independent code. The different containers are different, and they have strengths and weaknesses that vary in significant ways. Item 3: Making copy cheap and correct for objects in containers. Copy in,copy out. That's the STL way. The slicing problem suggests that inserting a derived class object into a container...
2020-04-30 09:10:33
Item 1: Choose your containers with care.
Item 2: Beware the illusion of container-independent code.
The different containers are different, and they have strengths and weaknesses that vary in significant ways.
Item 3: Making copy cheap and correct for objects in containers.
Copy in,copy out. That's the STL way.
The slicing problem suggests that inserting a derived class object into a container of base class objects is almost always an error.
An easy way to make copying efficient, correct,and immune to the slicing problem is to create containers of pointers instead of containers of objects.
Item 4: Call empty instead of checking size() against zero.
Empty is a constant-time operation for all standard containers, but for some list implementations, list takes linear time.
Item 5: Prefer range member functions to their single-element counterparts.
It's generally less work to write the code using the range member functions.
Range member functions tend to lead to code that is clearer and more straightforward.
Range construction. Range insertion. Range erasure. Range assignment.
Item 6: Be alert for C++'s most vexing parse.
Item 7: When using containers of newed pointers, remember to delete the pointers before the container is destroyed.
STL containers are smart, but they are not smart enough to know whether to delete the pointes they contain.
Item 8: Never create containers of auto_ptrs.
Item 9: Choose carefully among erasing options.
Item 10: Be aware of allocator convections and restrictions.
Item 11: Understand the legitimate uses of custom allocators.
Item 12: Have realistic expectations about the thread safety of STL containers.
Multiple readers are safe. Multiple writers to different containers are safe.
Item 13: Prefer vector and string to dynamically allocated arrays.
Item 14: Use reserve to avoid unnecessary reallocations.
Item 15: Be aware of variations in string implementations.
Item 16: Know how to pass vector and string data to legacy APIs.
Item 17: Use "the swap trick" to trim excess capacity.
Item 18: Avoid using vector<bool>.
Item 19: Understand the difference between equality and equivalence.
Item 20: Specify comparison types for associative containers of pointers.
Item 21: Always have comparison functions return false for equal values.
Item 22: Avoid in-place key modification in set and mulitset.
Item 23: Consider replacing associative containers with sorted vectors.
Item 24: Choose carefully between map::operator[ ] and map::insert when efficiency is important.
Item 25: Familiarize yourself with the nonstandard hashed containers.
Item 26: Prefer iterator to const_iterator,reverse_iterator,and const_reverse_iterator.
Item 27: Use distance and advance to convert a container's const_iterators to iterators.
Item 28: Understand how to use a reverse_iterator's base iteator.
Item 29: Consider istreambuf_iterators for character-by-character input.
Item 30: Make sure destination ranges are big enough.
Item 31: Know your sorting options.
Item 32: Follow remove-like algorithms by erase if you really want to remove something.
Item 33: Be wary of remove-like algortithms on containers of pointers.
Item 34: Note which algorithms expect sorted ranges.
Item 35: Implement simple case-insensitive string comparisons via mismatch or lexicographical_compare.
Item 36: Understand the proper implementation of copy_if.
Item 37: Use accumulate or for_each to summarize ranges.
Item 38: Design functor classes for pass-by-value.
* If you need to perform a full sort on a vector, string, deque, or array, you can use sort or stable_sort. * If you have a vector, string, deque, or array and you need to put only the top n elements in order, partial_sort is available. * If you have a vector, string, deque, or array and you need to identify the element at position n or you need to identify the top n elements without putting th...
2015-11-22 16:23:25
* If you need to perform a full sort on a vector, string, deque, or array, you can use sort or stable_sort.
* If you have a vector, string, deque, or array and you need to put only the top n elements in order, partial_sort is available.
* If you have a vector, string, deque, or array and you need to identify the element at position n or you need to identify the top n elements without putting them in order. nth_element is at your beck and call.
* If you need to separate the elements of a standard sequence container or an array into those that do and do not satisfy some criterion, you're probably looking for partition or stable_partition.
* If your data is in a list, you can use partition and stable_partition directly, and you can use list-sort in place of sort and stable_sort. If you need the effects offered by partial_sort or nth_element, you'll have to approach the task indirectly, but there are a number of options, as I sketched above.引自 Item31.了解各种与排序有关的选择
advance(i, distance<ConstIter>(i, ci)); It's as efficient as the iterators allow it to be. For random access iterators (such as those sported by vector,string, and deque), it's a constant-time operation. For bidirectional iterators (i.e., those for all other standard containers and for some implementations of the hashed containers (see Item 25)), it's a linear-time operation.
2015-11-22 15:47:17
advance(i, distance<ConstIter>(i, ci));
It's as efficient as the iterators allow it to be. For random access iterators (such as those sported by vector,string, and deque), it's a constant-time operation. For bidirectional iterators (i.e., those for all other standard containers and for some implementations of the hashed containers (see Item 25)), it's a linear-time operation.引自 item27.使用distance和advance将容器的const_iterator转换成iterator
To avoid resource leaks when you have containers of pointers that should be deleted, you must either replace the pointers with smart reference-counting pointer objects (such as Boost's shared_ptr) or you must manually delete each pointer in the container before the container is destroyed.
2015-11-15 20:34:20
To avoid resource leaks
when you have containers of pointers that should be deleted, you must either replace
the pointers with smart reference-counting pointer objects (such as Boost's shared_ptr)
or you must manually delete each pointer in the container before the container is
destroyed.引自 Item 7.如果容器中包含了通过new创建的指针,切记在容器对象析构前将指针delete掉
1.the erase-remove idiom is the best way to get rid of elements with a specific value when c is a vector, string, or deque. vector<int> c; //...... c.erase( remove(c.begin(), c.end(), 1963), c.end());delete all 1963 elements 2.the remove member function is the best way to get rid of elements with a specific value when c is a list. list<int> c; //...... c. remove(1963);delete all 196...
2015-11-15 21:06:36
1.the erase-remove idiom is the best way to get rid of elements with a specific value when c is a vector, string, or deque.引自 Item9.慎重选择删除元素的方法
vector<int> c;
//......
c.erase( remove(c.begin(), c.end(), 1963), c.end());delete all 1963 elements
2.the remove member function is the best way to get rid of elements with a specific value when c is a list.引自 Item9.慎重选择删除元素的方法
list<int> c;
//......
c. remove(1963);delete all 1963 elements
*To eliminate all objects in a container that have a particular value: If the container is a vector, string, or deque, use the erase-remove idiom. If the container is a list, use list::remove. If the container is a standard associative container, use its erase member function. *To eliminate all objects in a container that satisfy a particular predicate: If the container is a vector, string, or ...
2015-11-15 21:18:14
*To eliminate all objects in a container that have a particular value:
If the container is a vector, string, or deque, use the erase-remove idiom.
If the container is a list, use list::remove.
If the container is a standard associative container, use its erase member function.
*To eliminate all objects in a container that satisfy a particular predicate:
If the container is a vector, string, or deque, use the erase-remove_if idiom.
If the container is a list, use list::remove_if.
If the container is a standard associative container, use remove_copy_if and swap, or write a loop to walk the container elements, being sure to postincrement your iterator when you pass it to erase.
*To do something inside the loop (in addition to erasing objects):
If the container is a standard sequence container, write a loop to walk the container elements, being sure to update your iterator with erase's return value each time von call it.
If the container is a standard associative container, write a loop to walk the container elements, being sure to postincrement your iterator when you pass it
to erase.引自 Item9.慎重选择删除元素的方法
vector<Contestant>(contestants).swap(contestants); string(s).swap(s); The expression vector<Contestant>(contestants) creates a temporary vector that is a copy of contestants: vector's copy constructor does the work. However, vector's copy constructor allocates only as much memory as is needed for the elements being copied, so this temporary vector has no excess capacity. We then swa...
The expression vector<Contestant>(contestants) creates a temporary vector that is a copy of contestants: vector's copy constructor does the work. However, vector's copy constructor allocates only as much memory as is needed for the elements being copied, so this temporary vector has no excess capacity. We then swap the data in the temporary vector with that in contestants, and by the time we're done, contestants has the trimmed capacity of the temporary, and the temporary holds the bloated capacity that used to be in contestants. At that point (the end of the statement), the temporary vector is destroyed, thus freeing the memory formerly used by contestants.引自 Item17:使用"swap技巧"去除多余的容量
• The standard STL sequence containers, vector, string, deque, and list. • The standard STL associative containers, set, multiset, map and multimap. • The nonstandard sequence containers slist and rope. slist is a singly linked list, and rope is essentially a heavy-duty string. • The nonstandard associative containers hash_set, hash_multiset, hash_map, and hash_mul...
2020-06-19 23:30:04
• The standard STL sequence containers, vector, string, deque, and list.
• The standard STL associative containers, set, multiset, map and multimap.
• The nonstandard sequence containers slist and rope. slist is a singly linked list, and rope is essentially a heavy-duty string.
• The nonstandard associative containers hash_set, hash_multiset, hash_map, and hash_multimap.
Contiguous-memory containers (also known as array-based containers] store their elements in one or more (dynamically allocated) chunks of memory, each chunk holding more than one container element. If a new element is inserted or an existing element is erased, other elements in the same memory chunk have to be shifted up ordown to make room for the new element or to fill the space formerly occupied by theerased element. This kind of movement affects both performance (see Items 5 and 14) and exception safety (as we'll soon see). The standard contiguous-memory containers are vector, string, and deque. The nonstandard rope is also a contiguous-memory container.
Node-based containers store only a single element per chunk of (dynamically allocated) memory. Insertion or erasure of a container element affects only pointers to nodes, not the contents of the nodes themselves, so element values need not be moved when something is inserted or erased. Containers representing linked lists, such as list and slist, are node-based, as are all the standard associative containers. typically implemented as balanced trees.)
Item 1: Choose your containers with care. Item 2: Beware the illusion of container-independent code. The different containers are different, and they have strengths and weaknesses that vary in significant ways. Item 3: Making copy cheap and correct for objects in containers. Copy in,copy out. That's the STL way. The slicing problem suggests that inserting a derived class object into a container...
2020-04-30 09:10:33
Item 1: Choose your containers with care.
Item 2: Beware the illusion of container-independent code.
The different containers are different, and they have strengths and weaknesses that vary in significant ways.
Item 3: Making copy cheap and correct for objects in containers.
Copy in,copy out. That's the STL way.
The slicing problem suggests that inserting a derived class object into a container of base class objects is almost always an error.
An easy way to make copying efficient, correct,and immune to the slicing problem is to create containers of pointers instead of containers of objects.
Item 4: Call empty instead of checking size() against zero.
Empty is a constant-time operation for all standard containers, but for some list implementations, list takes linear time.
Item 5: Prefer range member functions to their single-element counterparts.
It's generally less work to write the code using the range member functions.
Range member functions tend to lead to code that is clearer and more straightforward.
Range construction. Range insertion. Range erasure. Range assignment.
Item 6: Be alert for C++'s most vexing parse.
Item 7: When using containers of newed pointers, remember to delete the pointers before the container is destroyed.
STL containers are smart, but they are not smart enough to know whether to delete the pointes they contain.
Item 8: Never create containers of auto_ptrs.
Item 9: Choose carefully among erasing options.
Item 10: Be aware of allocator convections and restrictions.
Item 11: Understand the legitimate uses of custom allocators.
Item 12: Have realistic expectations about the thread safety of STL containers.
Multiple readers are safe. Multiple writers to different containers are safe.
Item 13: Prefer vector and string to dynamically allocated arrays.
Item 14: Use reserve to avoid unnecessary reallocations.
Item 15: Be aware of variations in string implementations.
Item 16: Know how to pass vector and string data to legacy APIs.
Item 17: Use "the swap trick" to trim excess capacity.
Item 18: Avoid using vector<bool>.
Item 19: Understand the difference between equality and equivalence.
Item 20: Specify comparison types for associative containers of pointers.
Item 21: Always have comparison functions return false for equal values.
Item 22: Avoid in-place key modification in set and mulitset.
Item 23: Consider replacing associative containers with sorted vectors.
Item 24: Choose carefully between map::operator[ ] and map::insert when efficiency is important.
Item 25: Familiarize yourself with the nonstandard hashed containers.
Item 26: Prefer iterator to const_iterator,reverse_iterator,and const_reverse_iterator.
Item 27: Use distance and advance to convert a container's const_iterators to iterators.
Item 28: Understand how to use a reverse_iterator's base iteator.
Item 29: Consider istreambuf_iterators for character-by-character input.
Item 30: Make sure destination ranges are big enough.
Item 31: Know your sorting options.
Item 32: Follow remove-like algorithms by erase if you really want to remove something.
Item 33: Be wary of remove-like algortithms on containers of pointers.
Item 34: Note which algorithms expect sorted ranges.
Item 35: Implement simple case-insensitive string comparisons via mismatch or lexicographical_compare.
Item 36: Understand the proper implementation of copy_if.
Item 37: Use accumulate or for_each to summarize ranges.
Item 38: Design functor classes for pass-by-value.
* If you need to perform a full sort on a vector, string, deque, or array, you can use sort or stable_sort. * If you have a vector, string, deque, or array and you need to put only the top n elements in order, partial_sort is available. * If you have a vector, string, deque, or array and you need to identify the element at position n or you need to identify the top n elements without putting th...
2015-11-22 16:23:25
* If you need to perform a full sort on a vector, string, deque, or array, you can use sort or stable_sort.
* If you have a vector, string, deque, or array and you need to put only the top n elements in order, partial_sort is available.
* If you have a vector, string, deque, or array and you need to identify the element at position n or you need to identify the top n elements without putting them in order. nth_element is at your beck and call.
* If you need to separate the elements of a standard sequence container or an array into those that do and do not satisfy some criterion, you're probably looking for partition or stable_partition.
* If your data is in a list, you can use partition and stable_partition directly, and you can use list-sort in place of sort and stable_sort. If you need the effects offered by partial_sort or nth_element, you'll have to approach the task indirectly, but there are a number of options, as I sketched above.引自 Item31.了解各种与排序有关的选择
advance(i, distance<ConstIter>(i, ci)); It's as efficient as the iterators allow it to be. For random access iterators (such as those sported by vector,string, and deque), it's a constant-time operation. For bidirectional iterators (i.e., those for all other standard containers and for some implementations of the hashed containers (see Item 25)), it's a linear-time operation.
2015-11-22 15:47:17
advance(i, distance<ConstIter>(i, ci));
It's as efficient as the iterators allow it to be. For random access iterators (such as those sported by vector,string, and deque), it's a constant-time operation. For bidirectional iterators (i.e., those for all other standard containers and for some implementations of the hashed containers (see Item 25)), it's a linear-time operation.引自 item27.使用distance和advance将容器的const_iterator转换成iterator
0 有用 泽洋 2013-11-20 19:41:05
第一遍
0 有用 kkkkk 2019-11-17 21:58:47
读之前常常自嘲自己不会cpp,只会c with STL。读完发现其实我对STL一无所知😂
1 有用 胖达子 2015-07-21 14:38:08
文字比较啰嗦
0 有用 散关清渭 2019-07-23 13:47:08
虽然有点儿老了 但是依旧是好书 对我这种C++ Beginner 很有意义
0 有用 lichray 2012-07-07 18:02:15
很多细节不是光读标题就能理解的!
0 有用 Ethanity 2020-01-01 12:36:52
2019年的最后一本technical。读了(以及在读)effective系列的其他三本 这本提供的惊喜最少。首先是内容太老 C++11及之后的STL更新对cpp编程的影响还是显著的 meyers该更新书的内容了。其次另三本effective告诉你“原来该这么做” C++STL则是“原来有这个” 各种TMP眼花缭乱。这本相对就平庸了些 也可能是我STL用的不够细致。
0 有用 kkkkk 2019-11-17 21:58:47
读之前常常自嘲自己不会cpp,只会c with STL。读完发现其实我对STL一无所知😂
0 有用 散关清渭 2019-07-23 13:47:08
虽然有点儿老了 但是依旧是好书 对我这种C++ Beginner 很有意义
0 有用 女王公园的八神 2019-02-19 11:51:34
好书,深入浅出,学习STL必读 查漏补缺,用STL还不熟练,加油!加油!这是一本好书,配合STL的源码一起阅读。我最近还是多看看别人写的代码,学习别人的思维模式。ps, 这本书适合对STL理解深入,《C++ Primer Plus》还是没有仔细讨论STL。pps,看的英文版本的,中文版本字都重叠起来了lol
0 有用 apprentice 2018-12-22 13:55:52
延续了Scott Meyers的一贯的讲解思路 但是内容的实效性不够 很多functoins 比如bind1st mem_fun都已经被lambda取代 并没有延展讲解的需要