Effective STL中文版

[美] Scott Meyers

出版时间

2006-04-01

ISBN

9787302126959

评分

★★★★★
书籍介绍
这不是一本教你如何使用STL的入门书,而是一本写给已经踩过坑的人的经验之书。Scott Meyers没有从容器、迭代器的基础用法讲起,而是直接切入那些真正容易出问题的地方:为什么vector默认优于list,何时又必须切换;erase-remove idiom为何是清理元素的最佳方式;容器里存指针时如何避免内存泄漏;拷贝语义背后隐藏的容量与性能陷阱。全书50个条款,每个都追问“为什么”——分析问题的来源与解决方案的优劣,而非简单给出结论。它适合有一定STL使用经验、愿意反复查阅的读者,读时需要边读边实践,否则很容易“看完就忘”。它不是工具手册,而更像一份关于“如何正确使用STL”的圣经注释,值得放在手边随时翻检。
AI导读
核心看点
  • 50个条款详解STL使用陷阱与优化
  • 区分vector与list等容器的适用场景
  • 解析拷贝语义及智能指针防内存泄漏
读者共识
  • 经典必读,是STL用户从入门到精通的指南
  • 纠正大量野路子用法,让代码更标准高效
  • 虽部分技术过时,但核心设计思想依然极具价值
精彩摘录
  • "拷进去,拷出来。这就是STL的方式。... ,拷贝对象是STL的方式。"
  • "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."
  • "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."
  • "2.the remove member function is the best way to get rid of elements with a specific value when c is a list."
  • "*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 obje"
  • "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 capaci"
  • "That's why it makes sense to consider using a sorted vector instead of an associative container only when you know that your data structure is used in such a way that lookups are almost never mixed with insertions and erasures."
  • "If you're updating an existing map element, operator[] is preferable, but if you're adding a new element, insert has the edge."
用户评论
心血来潮mark下老早前读过的书
scott meyers “effective三部曲”之三
教人如何正确使用stl库的经验之谈,还不错的~~
一身冷汗,我是用了多少野路子的STL用法。后面某些 functor 在 c++ 11/14/17 里已经 deprecated ,需要找本新的书看了。当然,我们线上还是老旧的 gcc
经典,STL用户必读
快速了解stl的好书。书摘后续从博客整理到豆瓣上来。
不读过这本书不能谈自己会用STL
scott的有些地方自我矛盾,比如对于最佳实践的迭代器,一会说使用const,一会说使用一般迭代器,不知道是不是翻译的问题。确实是建议直接看STL源码
c++之复杂是我生平仅见
虽然有些条目确实是没意思,但是总体,真是,爽。读的时候心里一直在叫,我勒个去,还能这样。
下载
收藏