Chinaunix首页 | 论坛 | 博客
  • 博客访问: 511897
  • 博文数量: 174
  • 博客积分: 8001
  • 博客等级: 中将
  • 技术积分: 1840
  • 用 户 组: 普通用户
  • 注册时间: 2009-03-04 19:30
文章分类

全部博文(174)

文章存档

2011年(1)

2010年(24)

2009年(149)

我的朋友

分类: C/C++

2009-03-08 00:21:44

使用STL来编程的目的是充分避免重复性的工作,并且得到可靠的服务。
STL是C++标准,它的实现随着编译器的不同而不同。如果你使用的是VC,就是用VC的STL实现;用G++,就是用sgi的实现。虽然实现不同,但是接口是一致的。
今后将在sgi的STL文档和其他书籍,加上自己的实践而得到的理解做一个备忘。当然自己是希望能够理解数据结构和重要的一些算法,达到自己也可以做一个实现的程度。STL是工具,而不是目的。
下面是摘录自sgi的Introduction to the standard template library

it provides many of the basic algorithms and data structures of computer science.
数据结构和算法,可以说是编程的根本性的东西。

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.
STL往往使用range,而且是不对称的半闭集。

Concepts and Modeling

template <class InputIterator, class T>
      InputIterator find(InputIterator first, InputIterator last, const T& value) {
          while (first != last && *first != value) ++first;
          return first;
      }


不仅在STL领域,而是在范式编程领域,Concept指的是符合一系列操作要求的类型的集合,如上面的find函数,要求inputiterator至少能够使用!=, ++ 和 * 的操作,所以指针和迭代器可以被使用,就是因为它们都符合这个Concept;我们称它们为Model of this Concept。

Refinement

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 , is only a model of Input Iterator: it does not conform to the more stringent Bidirectional Iterator requirements. 

如果一个Concept在另一个Concept的要求之外还有其他的要求,那么我们说这个Concept是对原来的refinement,它是其中一个子集。类似在C++里的继承,但是继承是针对类型来说,在C++里并没有Concept和Model的术语,它是范式编程的术语。要理解下面这句话:

如果一种类型是某一个Concept的Model,而这个Concept是另一个Concept的Refinement,那么这种类型也是原来的Concept的一个Model。

Other parts of the STL

First, the STL includes several utilities: very basic concepts and functions that are used in many different parts of the library. The concept , 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. are very specialized, and you can safely ignore them for almost all purposes.

Finally, the STL includes a large collection of , 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 (a function object that takes a single argument, i.e. one that is called as f(x)) and (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. 





阅读(1507) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~