STL入门基础(三)

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

Iterators

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.

 

Container

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.

 Sequence

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

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. 

STL简介[这篇文章通俗易懂,特别适合没用过STL的初学者。]

STL 简介,标准模板库 作者:Scott Field    这篇文章是关于C++语言的一个新的扩展——标准模板库的(Standard Template Library),也叫STL。     当我第...
  • lwd2k
  • lwd2k
  • 2002年08月26日 11:44
  • 696

Android 之小技巧

小技巧,不足为专题内容 TextView的drawableLeft与文本一起居中显示原始的TextView设置了LeftDrawable后,如果TextView的宽度是MATCH_PARENT,Lef...
  • xude1985
  • xude1985
  • 2016年03月23日 18:18
  • 186

第15课:Spark Streaming源码解读之No Receivers彻底思考

receive和no receiver的方式(derict的方式) 封装器一定是RDD类型的KafkaRDD,是为不同的数据来源推出不同的RDD foreachRDD中就可以获得当前batch d...
  • luobailian
  • luobailian
  • 2016年05月31日 16:52
  • 488

关于"net start mysql"提示"服务名无效"的启动mysql方法

关于"net start mysql"提示"服务名无效"的启动mysql方法
  • m0_37786859
  • m0_37786859
  • 2017年05月13日 12:02
  • 620

Android程序Crash时的异常上报

转载请注明来源:http://blog.csdn.net/singwhatiwanna/article/details/17289479 前言 大家都知道,android应用不可避免的会发生crash...
  • singwhatiwanna
  • singwhatiwanna
  • 2013年12月12日 22:38
  • 23311

移动前端,a new beginning

csdn前一阵子老是登陆不上去,最后才发现原来是被盗号,发了垃圾博客,被管理员关掉了。现在终于又回来了。这一次,是真的要继续写博客了。 今天刚办完了离职手续,从毕业就在这家公司,工作了三年了,因为公...
  • linxinyuluo
  • linxinyuluo
  • 2015年10月14日 21:18
  • 483

“Beginning Python”(八)“XML”

本文主要学习《Beginning Python》中的第三个实践项目“Project 3: XML for All Occasions”。这个项目主要向读者展示:python编程中XML的应用。就像这个...
  • Sagittarius_Warrior
  • Sagittarius_Warrior
  • 2017年07月27日 16:18
  • 145

C++ STL 一般总结

一、一般介绍       STL(Standard Template Library),即标准模板库,是一个具有工业强度的,高效的C++程序库。它被容纳于C++标准程序库(C++ Stand...
  • u013174512
  • u013174512
  • 2016年05月10日 21:57
  • 860

dll传递stl注意

转自:http://www.cnblogs.com/lidabo/archive/2013/09/04/3300327.html STL跨平台调用会出现很多异常,你可以试试. ...
  • z397164725
  • z397164725
  • 2013年09月11日 14:04
  • 2525

Unity导入STL格式模型(一)

STL文件是一种非常简单且实用的三角形网格文件,他只按照三角面片的方式存储了所有的面信息及法矢量,不包含纹理以及其他任何媒体信息,主要存储格式分为:ASCII码格式、二进制格式。这种文件格式在工业上以...
  • qq992817263
  • qq992817263
  • 2017年05月24日 13:49
  • 1968
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:STL入门基础(三)
举报原因:
原因补充:

(最多只允许输入30个字)