精通
英语
和
开源
,
擅长
开发
与
培训
,
胸怀四海
第一信赖
锐英源精品开源心得,转载请注明:“锐英源www.wisestudy.cn,孙老师作品,电话13803810136。”需要全文内容也请联系孙老师。
简介
STL provides a template based set of collection classes, and methods for working on those collections. The collection classes give the developer access to fast and efficient collections. While the methods, which are known as the algorithms, provide template based collection manipulations functions.
The benefits of STL include
STL提供了基于一套集合类的模板和在这些集合类上面工作的方法。集合类让开发人员能够访问快速,高效的集合。同时,STL也提供了众所周知的算法,该算法给基于集合的模板提供了操作函数。
STL的好处包括
模板
If you are already familiar with templates then skip to the next section. Otherwise read this section for a brief tutorial on templates. A template can be thought of as a macro with type checking. For example to declare a template we would do the following:
如果您已经熟悉使用模板,然后跳到下一节。对模板的简明教程,否则读这一节。模板可以被看作是有类型检查功能的宏。例如声明一个模板,我们将做到以下几点:
template < class T > class Value { T _value; public: Value() {}; Value ( T value ) { _value = value; } T getValue (); void setValue ( T value ); }; template < class T > T Value<T>::getValue () { return _value; } template < class T > void Value<T>::setValue ( T value ) { _value = value; }
This example declares a class Value, which stores a parameterized value, _value, of type T. After the keyword template, in the angled brackets, is a list of parameters. The list tells the template what types will be used in the template. A good analogy for the template parameter list is the parameter list for a class constructor. Like a constructor, the number of arguments for the template can be from one to many.
Methods for a template that are declared outside the class definition require the template keyword, as shown above. To use the Value class to declare an array of floats we would do:
这个例子声明了一个类Value,它存储一个参数值,_value的类型为 T 。在关键字template后,在尖括号内是一个参数列表。这个列表告诉将在模板中使用的是什么类型的模板 。模板参数列表可以类比理解为类的构造函数的参数列表。像是一个构造函数,模板参数的数量可以从一个到许多个。
一个模板类定义之外声明的方法需要template关键字,如上图所示。使用Value类定义的浮点数组如下:
Value<float> values[10]; / / 参数类型为float的Value类的数组
This declares an array of values, the angled brackets tells us that Value will store its value as a float.
If we wanted to declare a list to work with our template based Value class we could do the following:
这定义了一个Value类的数组,尖括号告诉我们,Value将以float类型存储其值。
如果我们想用Value模板类来定义列表,可用如下所示:
template < class T > class ValueList { Value<T> * _nodes. public: ValueList ( int noElements ) { _nodes = new Value<T>[noElements]; } virtual ~ValueList () { delete [] _nodes; } };
Here we have declared a template-based class that stores a variable sized list of values.
在这里,我们定义一个基于模板的类,它存储一个可变大小的列表。
STL集合类型
Each STL collection type has its own template parameters, which will be discussed later. What type of collection you use is up to your needs and tastes. From past experience, the vector and map classes are the most useful. The vector class is ideal for simple and complex collection types, while the map class is used when an associative type of collection is needed. The deque collection is excellent for use in systems that have queued based processing, such as a message based system.
每个STL集合类型都有它自己的模板参数,这将在后面讨论。您使用什么类型的集合,是您的需求和口味。从以往的经验,vector和map类是最有用的。Vector类是简单的和复杂的集合类型的理想选择,而map类在关联的集合类型情况下需要时使用。有排队加工为主的系统,如一个基于消息的系统,最好使用deque集合类。
vector
A collection of elements of type T.
list
A collection of elements of type T. The collection is stored as a bi-directional linked list of elements, each containing a member of type T.
To include the class definition use:
vector
T类型的元素的集合
list
T类型元素的集合。集合存储为一个双向链接的元素的列表,每个包含一个T类型的成员
要包括类定义,请使用:
#include <list>