# 标准模板库

vector<int> v(3);           //声明一个有三个元素的vector

v[0] = 7;

v[1] = v[0]+3;

v[2] = v[0]+v[1];          //v[0]=7,v[1]=10,v[2]=17

STL也包含了大量的算法来操纵存储在容器中数据.例如,通过使用reverse算法,你可以反转一个vector中的元素顺序.

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

double A[8] = {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];

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.

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 && *fist != value) ++first;

Return first;

}

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.

Find接受三个参数:两个规定了一个范围的迭代器和在该范围内要搜寻的一个值value.它在[first,last]范围内检查每一个迭代器,从开始到最后,当它找到一个指向value的迭代器或者达到末尾时就停止了.

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.

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.

• 本文已收录于以下专栏：

## C++标准模板库STL算法与自适应容器（栈和队列）

• LG1259156776
• 2015年08月06日 13:22
• 1821

## C++ STL标准模板库

• qq563129582
• 2015年02月27日 16:32
• 1246

## 几个标准模板库常用功能示例

1. 对std::map按key查找 方法： bool map_finder_by_key(std::pair aPair, CString strKey) { return 0 == ...
• a_dev
• 2017年03月09日 15:52
• 242

## C++标准库和C++标准模版库

C++标准库很大,在现在的情况下,C++标准库确实越来越好,因为大的库会包含大量的功能.标准库中的功能越多,开发自己的应用程序时能借助的功能就越多,C++库并非提供一切(很明显的是没有提供开...
• Molany
• 2008年11月25日 16:55
• 4499

## C++ 标准模板库(STL)汇总

C++ 标准模板库(STL)汇总
• DaveBobo
• 2017年02月22日 21:08
• 775

## 使用标准模板库 (STL)之一

STL STL的一个重要特点是数据结构和算法的分离。尽管这是个简单的概念，但这种分离确实使得STL变得非常通用。例如，由于STL的sort()函数是完全通用的，你可以用它来操作几乎任何数据集合，包括...
• u010236550
• 2013年10月08日 21:53
• 1118

## 标准模板库STL详解之vector

• qq_33371343
• 2016年05月31日 10:49
• 360

## 标准模板库（list）

• fu_yunjian
• 2016年09月30日 16:35
• 219

## 用标准模板库定义小顶堆，求哈夫曼树带权和

• m0_37036984
• 2018年01月06日 14:51
• 52

## [C++标准模板库：自修教程与参考手册]关于vector

• m0_37316917
• 2017年07月20日 21:55
• 181

举报原因： 您举报文章：标准模板库 色情 政治 抄袭 广告 招聘 骂人 其他 (最多只允许输入30个字)