原创 2007年10月09日 14:13:00

study 的版本是 Release 3.3: June 8, 2000

SGI STL被GCC(linux下的C++编译器)所采用,你可以在GCC的Include子目录下找到所有头文件(比如:C:/cygnus/cygwin -b20/include/g++/include)。

Which compilers are supported?
The STL has been tested on these compilers: SGI 7.1 and later, or 7.0 with the -n32 or -64 flag; gcc 2.8 or egcs 1.x; Microsoft 5.0 and later.

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.

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.

      vector<int> v(3);            // Declare a vector of 3 elements.
      v[0] = 7;
      v[1] = v[0] + 3;
      v[2] = v[0] + v[1];          // v[0] == 7, v[1] == 10, v[2] == 17  

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.

      reverse(v.begin(), v.end()); // v[0] == 17, v[1] == 10, v[2] == 7

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.

      double A[6] = { 1.2, 1.3, 1.4, 1.5, 1.6, 1.7 };
      reverse(A, A + 6);
      for (int i = 0; i < 6; ++i)
        cout << "A[" << i << "] = " << A[i];

This example uses a range, just like the example of reversing a vector: 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.


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. 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>
      InputIterator find(InputIterator first, InputIterator last, const T& value) {
          while (first != last && *first != value) ++first;
          return first;

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.

      int* find(int* first, int* last, const int& value) {
          while (first != last && *first != value) ++first;
          return first;

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. 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.

Concepts are not a part of the C++ language;  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.



A Container is an object that stores other objects (its elements), and that has methods for accessing its elements. In particular, every type that is a model of Container has an associated iterator type that can be used to iterate through the Container's elements.

There is no guarantee that the elements of a Container are stored in any definite order; the order might, in fact, be different upon each iteration through the Container. Nor is there a guarantee that more than one iterator into a Container may be active at any one time. (Specific types of Containers, such as Forward Container, do provide such guarantees.)

A Container "owns" its elements: the lifetime of an element stored in a container cannot exceed that of the Container itself.


A Sequence is a variable-sized Container whose elements are arranged in a strict linear order. It supports insertion and removal of elements.

Associative Container

An Associative Container is a variable-sized Container that supports efficient retrieval of elements (values) based on keys. It supports insertion and removal of elements, but differs from a Sequence in that it does not provide a mechanism for inserting an element at a specific position.


Iterators are a generalization of pointers: they are objects that point to other objects. As the name suggests, iterators are often used to iterate over a range of objects: if an iterator points to one element in a range, then it is possible to increment it so that it points to the next element.

Iterators are central to generic programming because they are an interface between containers and algorithms: algorithms typically take iterators as arguments, so a container need only provide a way to access its elements using iterators. This makes it possible to write a generic algorithm that operates on many different kinds of containers, even containers as different as a vector and a doubly linked list.

The STL defines several different concepts related to iterators, several predefined iterators, and a collection of types and functions for manipulating iterators.

Function objects

A Function Object, or Functor (the two terms are synonymous) is simply any object that can be called as if it is a function. An ordinary function is a function object, and so is a function pointer; more generally, so is an object of a class that defines operator().

Carpicorn:here is the overview about the STL. The next section is the studying from Container Classes. 


模板是一种能够产生代码的代码. 它能够节省我们的代码量.熟悉使用可以提高开发效率。
  • 2017年12月30日 22:27


  • csdnxw
  • csdnxw
  • 2005-10-07 20:16:00
  • 5702

C with STL入门详解(适合初学者)

1.STL是什么? STL是Standard Template Library的简称,中文名标准模板库。简单来说就是封装好的一些组件。对于咱们考试而言,重要的有容器(containers)和算法(a...
  • mMingfunnyTree
  • mMingfunnyTree
  • 2018-01-27 17:52:37
  • 700


1.       STL 是什么 作为一个C++ 程序设计者,STL 是一种不可忽视的技术。 Standard Template Library (STL) :标准模板库, 更准确的说是 C++ 程...
  • a20102110080212
  • a20102110080212
  • 2013-09-14 11:52:39
  • 2767


1 检索与排序 #include sort函数可以对数组和vector数组进行排序,对于数组sort(a,a+n),对于vector,vector(b.begin,b.end) lower_bound...
  • qq_32225779
  • qq_32225779
  • 2016-09-17 16:04:45
  • 84

C with STL入门详解(适合初学者)

1.STL是什么? STL是Standard Template Library的简称,中文名标准模板库。简单来说就是封装好的一些组件。对于咱们考试而言,重要的有容器(containers)和算法(a...
  • mMingfunnyTree
  • mMingfunnyTree
  • 2018-01-27 17:52:37
  • 700


一、STL简介 STL(Standard Template Library,标准模板库)是惠普实验室开发的一系列软件的统称。它是由Alexander Stepanov、Meng Lee和David ...
  • lzm18064126848
  • lzm18064126848
  • 2016-01-07 11:56:19
  • 354


1、vector容器与iterator迭代器#include "vector.h" void main() { vector v;//定义vector容器 for(vector::it...
  • dazhushenxu
  • dazhushenxu
  • 2017-09-23 22:23:32
  • 106


1:给出n个字符串,输出每个字符串是第几个出现的字符串?多组数据 2:对每组数据,第一行输入n表示接下来有n个字符串 1 接下来的n行,每行输入一个非空的且长度最多为10的仅由大写字母组成的字符...
  • M___er
  • M___er
  • 2016-04-20 10:06:47
  • 205

STL学习笔记1— —STL简述

STL(Standard Template Library)里有很多组成部分,但是主要有三个,容器、迭代器和算法 容器用来管理某个特定对象的集合。每一种容器都有自己的优点和缺点,在项目中根据不同的需...
  • u013776188
  • u013776188
  • 2017-07-12 17:40:47
  • 64