---
title: "Scott Meyers的新书 Effective STL: 提高STL使用技术的50招"
author: "varsoft"
date: "2001-05-12T23:04:00Z"
tags: []
slug: "scott-meyers-effective-stl-stl-50"
draft: false
lastmod: "2001-05-12T23:04:00Z"
id: 873369
url: "https://varsoft.iteye.com/blog/873369"
---

Effective C++/More Effective C++的作者Scott Meyers即将推出最新力作：Effective STL, 该书目前已经完成，相信不久可以问世。但是与中国读者见面恐怕还是要在苦等之后。Addison=Wesley已经刊出了此书的页面。地址http://cseng.aw.com/book/toc/0,3830,0201749629,00.html

下面是该书的内容：  
某些条款我基本可以理解，大部分知道应该如此，但不知其所以然，某些条款令我相当诧异，所以更希望能够早日拜读。我对于某些条款的感想用\[]写在下面，我相信也是Meyers书中能够解释的东西。如果对于STL感兴趣的网友能够就这些条款展开讨论，我相信是非常有帮助的。

1\. Containers

Item 1: Choose your containers with care.

Item 2: Beware the illusion of container-independent code.  
\[值得注意的条款！]

Item 3: Make copying cheap and correct for objects in containers.  
\[可以理解，可是如何做到cheap，哪些情况下容易作出不正确的copy操作？]

Item 4: Call empty instead of checking size against zero.  
\[何故？]

Item 5: Prefer range member functions to their single-element counterparts.  
\[何故？]

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.  
\[可以理解]

Item 8: Never create containers of auto\_ptrs.  
\[可以理解]

Item 9: Choose carefully among erasing options.  
\[非常令我感兴趣的条款]

Item 10: Be aware of allocator conventions and restrictions.  
\[好深入的话题！]

Item 11: Understand the legitimate uses of custom allocators.  
\[怎么做？我略知一二，希望强化这方面的知识]

Item 12: Have realistic expectations about the thread safety of STL containers.  
\[呵呵，重要条款]

2\. Vector and string.

Item 13: Prefer vector and string to dynamically allocated arrays.  
\[对了，basic\_string本身就是一个动态数组容器，大家别以为只有vector]

Item 14: Use reserve to avoid unnecessary reallocations.  
[太对了，vector一旦发现容量(capacity)不足，会以惊人的魄力re-allocate，  
要当心，不然很惨]

Item 15: Be aware of variations in string implementations.  
\[愿洗耳恭听]

Item 16: Know how to pass vector and string data to legacy APIs.  
[Viva! I know a little bit about that..., but wait a minute, 根据我看  
Effective C++的经验，千万别以为你知道不少，Meyers会狠狠教训你，让你知道  
什么叫做井观天]

Item 17: Use "the swap trick" to trim excess capacity.  
\[哈，我一点都不懂！]

Item 18: Avoid using vector.  
[惊诧！完全打击！我一直把vector当首选容器！Meyers，我希望你给我个  
不用vector的理由先]

3\. Associative Containers.

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.  
\[啊？又一个晴天霹雳！简直匪夷所思嘛！不过想想，啊，Meyers真伟大]

Item 22: Avoid in-place key modification in set and multiset.  
\[不好意思，从来没正儿八经用过set]

Item 23: Consider replacing associative containers with sorted vectors.  
\[好建议！]

Item 24: Prefer map::insert to map::operator\[] when efficiency is a concern.  
\[嘿嘿，虽然没仔细想过这个问题，但是我可是一直都使用insert的哟！道理吗， 好想知道那么一点点，不过还是谨慎点好，我怕Meyers又来敲我的头...]

Item 25: Familiarize yourself with the nonstandard hashed containers.  
\[弱点！我还是快去补补课吧！]

4\. Iterators.

Item 26: Prefer iterator to const\_iterator, reverse\_iterator, and const\_reverse\_iterator.  
\[晴天霹雳3！不是说要尽可能地用const吗，怎么加了个下划线就不认帐了？不行， 您得给我讲清楚...]

Item 27: Use distance and advance to convert const\_iterators to iterators.  
\[丈二和尚]

Item 28: Understand how to use a reverse\_iterator's base iterator.  
\[更是连概念都搞不清楚了，reverse\_iterator还有base iterator? 赶快去查查书]

Item 29: Consider istreambuf\_iterators for character by character input.  
\[唔，值得好好研究]

5\. Algorithms.

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 algorithms 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: Use not1 and remove\_copy\_if to perform a copy\_if.  
\[以前在comp.lang.c++.moderated上读到过关于这个条款的故事，很精彩的！Scott Meyers 和Matt Austern都在这个问题上不小心栽了跟头，最后还是Bjarne Stroustrup一锤定音。等毕业论文改完了讲给大家听。]

Item 37: Use accumulate or for\_each to summarize sequences  
\[谨遵师命]

6\. Functors, Functor Classes, Functions, etc.

Item 38: Design functor classes for pass-by-value.  
\[概念上有点糊涂]

Item 39: Make predicates pure functions.  
\[晴天霹雳4，Stroustrup不是说Functor可以产生更高效的inline代码吗？为什么predications要例外呢? 俺要个说法。]

Item 40: Make functor classes adaptable.  
\[这个问题跟条款36相关的，看来Meyers真的从copy\_if的教训中挖掘出不少宝藏]

Item 41: Understand the reasons for ptr\_fun, mem\_fun, and mem\_fun\_ref.  
\[表面上是know了，但是老也想不起用它们来，说明还没有understand]

Item 42: Make sure less means operator&lt;.  
\[从来没想到过这个问题有什么重要性]

7\. Programming with the STL.

Item 43: Prefer algorithm calls to hand-written loops.  
\[我虽然从来都是如此这般作，却不知出了使自己的代码看起来更须cool以外还有什么别的道理]

Item 44: Prefer member functions to algorithms with the same names.  
\[算不上是晴天霹雳，但也很出乎我的意料，我一直以用纯algorithms操作contain为荣]

Item 45: Distinguish among count, find, binary\_search, lower\_bound, upper\_bound, and equal\_range.  
\[好像略知一二]

Item 46: Consider function objects instead of functions as algorithm parameters.  
\[这跟条款39不矛盾吗？]

Item 47: Avoid producing write-only code.  
\[不懂]

Item 48: Always #include the proper headers.

Item 49: Learn to decipher STL-related compiler diagnostics.  
\[谈何容易，尤其是VC]

Item 50: Familiarize yourself with STL-related web sites.  
\[知道一些，还不令我满意]