Team LiB
Previous Section Next Section

Chapter Summary

小结

One of the more important contributions from the standardization process for C++ was the creation and expansion of the standard library. The containers and algorithms libraries are a cornerstone of the standard library. The library defines more than 100 algorithms. Fortunately, the algorithms have a consistent architecture, which makes learning and using them easier.

C++ 标准化过程做出的更重要的贡献之一是:创建和扩展了标准库。容器和算法库是标准库的基础。标准库定义了超过一百个算法。幸运的是,这些算法具有相同的结构,使它们更易于学习和使用。

The algorithms are type independent: They generally operate on a sequence of elements that can be stored in a library container type, a built-in array, or even a generated sequence, such as by reading or writing to a stream. Algorithms achieve their type independence by operating in terms of iterators. Most algorithms take a pair of iterators denoting a range of elements as the first two arguments. Additional iterator arguments might include an output iterator denoting a destination, or another iterator or iterator pair denoting a second input sequence.

算法与类型无关:它们通常在一个元素序列上操作,这些元素可以存储在标准库容器类型、内置数组甚至是生成的序列(例如读写流所生成的序列)上。算法基于迭代器操作,从而实现类型无关性。大多数算法使用一对指定元素范围的迭代器作为其头两个实参。其他的迭代器实参包括指定输出目标的输出迭代器,或者用于指定第二个输入序列的另一个或一对迭代器。

Iterators can be categorized by the operations that they support. There are five iterator categories: input, output, forward, bidirectional, and random access. An iterator belongs to a particular category if it supports the operations required for that iterator category.

迭代器可通过其所支持的操作来分类。标准库定义了五种迭代器类别:输入、输出、前向、双向和随机访问迭代器。如果一个迭代器支持某种迭代器类别要求的运算,则该迭代器属于这个迭代器类别。

Just as iterators are categorized by their operations, iterator parameters to the algorithms are categorized by the iterator operations they require. Algorithms that only read their sequences often require only input iterator operations. Those that write to a destination iterator often require only the actions of an output iterator, and so on.

正如迭代器根据操作来分类一样,算法的迭代器形参也通过其所要求的迭代器操作来分类。只需要读取其序列的算法通常只要求输入迭代器的操作。而写目标迭代器的算法则通常只要求输出迭代器的操作,依此类推。

Algorithms that look for a value often have a second version that looks for an element for which a predicate returns a nonzero value. For such algorithms, the name of the second version has the suffix _if. Similarly, many algorithms provide so-called copying versions. These write the (transformed) elements to an output sequence rather than writing back into the input range. Such versions have names that end with _copy.

查找某个值的算法通常提供第二个版本,用于查找使谓词函数返回非零值的元素。对于这种算法,第二个版本的函数名字以 _if 后缀标识。类似地,很多算法提供所谓的复制版本,将(修改过的)元素写到输出序列,而不是写回输入范围。这种版本的名字以 _copy 结束。

A third pattern is whether algorithms read, write, or reorder elements. Algorithms never directly change the size of the sequences on which they operate. (If an argument is an insert iterator, then that iterator might add elements, but the algorithm does not do so directly.) They may copy elements from one position to another but cannot directly add or remove elements.

第三种模式是考虑算法是是否对元素读、写或者重新排序。算法从不直接改变它所操纵的序列的大小。(如果算法的实参是插入迭代器,则该迭代器会添加新元素,但算法并不直接这么做。)算法可以从一个位置将元素复制到另一个位置,但不直接添加或删除元素。


Team LiB
Previous Section Next Section