STL介绍-- 发布日期:2008-04-13 15:36

原创 2008年10月02日 18:25:00

翻译的一篇SGI STL文档还要好多不太会翻译,以后在慢慢领悟吧!

Introduction to the Standard Template Library


The Standard Template Library, or STL, is a C++ library of container classes, algorithms, and iterators; it provides many of the basic algorithms and data structures of computer science. The STL is a generic library, meaning that its components are heavily parameterized: almost every component in the STL is a template. You should make sure that you understand how templates work in C++ before you use the STL.


Containers and algorithms


Like many class libraries, the STL includes container classes: classes whose purpose is to contain other objects. The STL includes the classes vector, list, deque, set, multiset, map, multimap, hash_set, hash_multiset, hash_map, and hash_multimap. Each of these classes is a template, and can be instantiated to contain any type of object. You can, for example, use a vector<int> in much the same way as you would use an ordinary C array, except that vector eliminates the chore of managing dynamic memory allocation by hand.

像许多类库,STL包括容器类:用于包含其他类对象的类。STL包括的容器类有vector, list, deque, set, multiset, map, multimap, hash_set, hash_multiset, hash_map,hash_multimap。每一个容器类都是模板,而且可以被实例化去包含任何类型的对象。例如你可以使用vector<int>像使用一个原生的C数组,出此之外vector还消除了一些手动动态内存管理的琐碎事情。

The STL also includes a large collection of algorithms that manipulate the data stored in containers. You can reverse the order of elements in a vector, for example, by using the reverse algorithm.


There are two important points to notice about this call to reverse. First, it is a global function, not a member function. Second, it takes two arguments rather than one: it operates on a range of elements, rather than on a container. In this particular case the range happens to be the entire container v.


The reason for both of these facts is the same: reverse, like other STL algorithms, is decoupled from the STL container classes. This means that reverse can be used not only to reverse elements in vectors, but also to reverse elements in lists, and even elements in C arrays. The following program is also valid.


This example uses a range, just like the example of reversing a vector: the first argument to reverse is a pointer to the beginning of the range, and the second argument points one element past the end of the range. This range is denoted [A, A + 6); the asymmetrical notation is a reminder that the two endpoints are different, that the first is the beginning of the range and the second is one past the end of the range.




In the example of reversing a C array, the arguments to reverse are clearly of type double*. What are the arguments to reverse if you are reversing a vector, though, or a list? That is, what exactly does reverse declare its arguments to be, and what exactly do v.begin() and v.end() return?


The answer is that the arguments to reverse are iterators, which are a generalization of pointers. Pointers themselves are iterators, which is why it is possible to reverse the elements of a C array. Similarly, vector declares the nested types iterator and const_iterator. In the example above, the type returned by v.begin() and v.end() is vector<int>::iterator. There are also some iterators, such as istream_iterator and ostream_iterator, that aren't associated with containers at all.

答案是传递给reverse的参数是迭代器,一个泛型指针。指向它们自己的是迭代器,为什么可以倒序C数组中的元素呢。类似地,vector声明了内嵌类型的iterator and const_iterator。在上面的例子中,v.begin()v.end()返回的类型是vector<int>::iterator。还有一些其他的迭代器,比如istream_iteratorostream_iterator,它们并没有和容器相关联。

Iterators are the mechanism that makes it possible to decouple algorithms from containers: algorithms are templates, and are parameterized by the type of iterator, so they are not restricted to a single type of container. Consider, for example, how to write an algorithm that performs linear search through a range. This is the STL's find algorithm.


      template <class InputIterator, class T>

Find takes three arguments: two iterators that define a range, and a value to search for in that range. It examines each iterator in the range [first, last), proceeding from the beginning to the end, and stops either when it finds an iterator that points to value or when it reaches the end of the range.

First and last are declared to be of type InputIterator, and InputIterator is a template parameter. That is, there isn't actually any type called InputIterator: when you call find, the compiler substitutes the actual type of the arguments for the formal type parameters InputIterator and T. If the first two arguments to find are of type int* and the third is of type int, then it is as if you had called the following function.


Concepts and Modeling


One very important question to ask about any template function, not just about STL algorithms, is what the set of types is that may correctly be substituted for the formal template parameters. Clearly, for example, int* or double* may be substituted for find's formal template parameter InputIterator. Equally clearly, int or double may not: find uses the expression *first, and the dereference operator makes no sense for an object of type int or of type double. The basic answer, then, is that find implicitly defines a set of requirements on types, and that it may be instantiated with any type that satisfies those requirements. Whatever type is substituted for InputIterator must provide certain operations: it must be possible to compare two objects of that type for equality, it must be possible to increment an object of that type, it must be possible to dereference an object of that type to obtain the object that it points to, and so on.



Find isn't the only STL algorithm that has such a set of requirements; the arguments to for_each and count, and other algorithms, must satisfy the same requirements. These requirements are sufficiently important that we give them a name: we call such a set of type requirements a concept, and we call this particular concept Input Iterator. We say that a type conforms to a concept, or that it is a model of a concept, if it satisfies all of those requirements. We say that int* is a model of Input Iterator because int* provides all of the operations that are specified by the Input Iterator requirements.

STL算法中并不是只要Find有这样的一个需求集合,for_eachcount的参数,还要其他的算法同样需要满足同样的要求。这些需求是足够重要的,我们给他们起个名字:我们称这样的类型需求集合为概念,这样一个特殊的概念叫做输入迭代器。我们称满足这样要求的概念为一个模型。Int*是一个Input Iterator因为int*提供了所有Input Iterator要求的的操作。

Concepts are not a part of the C++ language; there is no way to declare a concept in a program, or to declare that a particular type is a model of a concept. Nevertheless, concepts are an extremely important part of the STL. Using concepts makes it possible to write programs that cleanly separate interface from implementation: the author of find only has to consider the interface specified by the concept Input Iterator, rather than the implementation of every possible type that conforms to that concept. Similarly, if you want to use find, you need only to ensure that the arguments you pass to it are models of Input Iterator. This is the reason why find and reverse can be used with lists, vectors, C arrays, and many other types: programming in terms of concepts, rather than in terms of specific types, makes it possible to reuse software components and to combine components together.



Input Iterator is, in fact, a rather weak concept: that is, it imposes very few requirements. An Input Iterator must support a subset of pointer arithmetic (it must be possible to increment an Input Iterator using prefix and postfix operator++), but need not support all operations of pointer arithmetic. This is sufficient for find, but some other algorithms require that their arguments satisfy additional requirements. Reverse, for example, must be able to decrement its arguments as well as increment them; it uses the expression --last. In terms of concepts, we say that reverse's arguments must be models of Bidirectional Iterator rather than Input Iterator.

Input Iterator事实上是一个非常弱的概念:它强加了很少的要求。一个Input Iterator必须提供一个指针算术的子集(它必须可以去通过前缀或者后缀的++递增一个Input Iterator),但是不需要提供所以的指针运算。这些对于find来说已经足够了,但是其他的一些算法需要一些额外的要求。例如Reverse必须可以通过--实现递减。按照概念的说法,我们说Reverse的参数为Bidirectional Iterator模型而非Input Iterator

The Bidirectional Iterator concept is very similar to the Input Iterator concept: it simply imposes some additional requirements. The types that are models of Bidirectional Iterator are a subset of the types that are models of Input Iterator: every type that is a model of Bidirectional Iterator is also a model of Input Iterator. Int*, for example, is both a model of Bidirectional Iterator and a model of Input Iterator, but istream_iterator, is only a model of Input Iterator: it does not conform to the more stringent Bidirectional Iterator requirements.

Bidirectional Iterator的概念和Input Iterator非常相似: 它仅仅加了一些额外的要求。Bidirectional Iterator的模型也是一个Input Iterator的子集:

任何Bidirectional Iterator 的模型同时也是一个Input Iterator 的模型。 例如, Int*,既是 Bidirectional Iterator 的模型也是一个Input Iterator 的模型, 但是istream_iterator仅仅是一个的模型Input Iterator: 它并没有符合Bidirectional Iterator更严格的要求。

We describe the relationship between Input Iterator and Bidirectional Iterator by saying that Bidirectional Iterator is a refinement of Input Iterator. Refinement of concepts is very much like inheritance of C++ classes; the main reason we use a different word, instead of just calling it "inheritance", is to emphasize that refinement applies to concepts rather than to actual types.

我们描述Input IteratorBidirectional Iterator的关系时说Bidirectional Iterator是一个更精巧的InputIterator。精巧概念非常像C++类的派生,我们用一个不同的词的主要原因是强调精巧是应用与概念而不是实际的类型。

There are actually three more iterator concepts in addition to the two that we have already discussed: the five iterator concepts are Output Iterator, Input Iterator, Forward Iterator, Bidirectional Iterator, and Random Access Iterator; Forward Iterator is a refinement of Input Iterator, Bidirectional Iterator is a refinement of Forward Iterator, and Random Access Iterator is a refinement of Bidirectional Iterator. (Output Iterator is related to the other four concepts, but it is not part of the hierarchy of refinement: it is not a refinement of any of the other iterator concepts, and none of the other iterator concepts are refinements of it.) The Iterator Overview has more information about iterators in general.

除了我们已经讨论的两个外实际上还要更多的迭代器类型:五种迭代器类型分别为Output Iterator, Input Iterator, Forward Iterator, Bidirectional IteratorRandom Access Iterator; Forward Iterator 是一个精巧的Input Itearot, Bidirectional Iterator是一个更精巧的 Forward Iterator, Random Access Iterator 是一个更精巧的Bidirectional Iterator. (Output Iterator 和其他四中迭代器相关,但不是派生和精巧关系) Iterator Overview 有更多的关于iterators的一般信息.

Container classes, like iterators, are organized into a hierarchy of concepts. All containers are models of the concept Container; more refined concepts, such as Sequence and Associative Container, describe specific types of containers.

容器类,像迭代器一样被组织进一个概念中。所以的容器都是概念Container的模型;更加精巧的概念,比如说Sequence and Associative Container, 描述了更特别的容器。


Other parts of the STL

If you understand algorithms, iterators, and containers, then you understand almost everything there is to know about the STL. The STL does, however, include several other types of components.

First, the STL includes several utilities: very basic concepts and functions that are used in many different parts of the library. The concept Assignable, for example, describes types that have assignment operators and copy constructors; almost all STL classes are models of Assignable, and almost all STL algorithms require their arguments to be models of Assignable.

Second, the STL includes some low-level mechanisms for allocating and deallocating memory. Allocators are very specialized, and you can safely ignore them for almost all purposes.

Finally, the STL includes a large collection of function objects, also known as functors. Just as iterators are a generalization of pointers, function objects are a generalization of functions: a function object is anything that you can call using the ordinary function call syntax. There are several different concepts relating to function objects, including Unary Function (a function object that takes a single argument, i.e. one that is called as f(x)) and Binary Function (a function object that takes two arguments, i.e. one that is called as f(x, y)). Function objects are an important part of generic programming because they allow abstraction not only over the types of objects, but also over the operations that are being performed.

C++ STL简单介绍

STL(Standard Template Library,标准模板库)是C++对泛型编程思想的实现,最早是惠普实验室开发的。 在被引入C++之前该技术就已经存在了非常长的一段时间。后来STL成为A...
  • w_16822
  • w_16822
  • 2016年10月14日 10:53
  • 262


摘自《effective stl》 关于STL的使用原则,以后还会再补充。 容器 条款1:仔细选择你的容器 条款2:小心对“容器无关代码”的幻想 条款3:使容器里对象的拷贝操作轻量而正...
  • losophy
  • losophy
  • 2016年06月12日 14:51
  • 703


STL算法主要是我们强大的标准库中以迭代器或数值或函数对象为参数预先定义好的一系列算法操作。      在STL算法分类中首先要提的就是两个普遍存在的后缀:      _if       ...
  • u011390632
  • u011390632
  • 2014年08月14日 15:17
  • 822


STL只能用来表示封闭的面或者体,stl文件有两种:一种是ASCII明码格式,另一种是二进制格式。 ASCII格式 ASCII码格式的STL文件逐行给出三角面片的几何信息,每一行以1个或2个关键字...
  • aiwen521125
  • aiwen521125
  • 2014年07月10日 20:59
  • 414

SQL SERVER 10进制转换36进制

--参数描述:@Hex10 被转换10进制数字 --测试方法:SELECT [dbo].[ConvertHex10To36](17122394) CREATE FUNCTION [dbo].[...
  • u010481729
  • u010481729
  • 2016年05月13日 10:53
  • 317

org.apache.hadoop.ipc.Client: Retrying connect to server

2011-08-10 09:57:16,000 INFO org.apache.hadoop.ipc.Client: Retrying connect to server: meimer/130.51...
  • ouyida3
  • ouyida3
  • 2014年11月03日 09:53
  • 1843


目录 STL算法概述 查找算法 堆算法 关系算法 集合算法 排列组合算法 排序和通用算法 删除和替换算法 生成和变异算法 算数算法 STL算法概述 简介: STL算法部分主要由头文件,,组成...
  • lishenglong666
  • lishenglong666
  • 2015年03月12日 11:05
  • 599

循环不变式--发表日期2008-04-13 01:51

//invariant:we have written r rows so r=0;//setting r to 0 makes the invariant true while(r!...
  • ToCpp
  • ToCpp
  • 2008年10月02日 18:24
  • 439

STL应用实例 一、vector的简单应用 [cpp] view ...
  • fdsafwagdagadg6576
  • fdsafwagdagadg6576
  • 2016年07月29日 09:32
  • 247

2015-11-17 15:18:36

  • u014180504
  • u014180504
  • 2015年11月17日 15:18
  • 297
您举报文章:STL介绍-- 发布日期:2008-04-13 15:36