[A summary of a types properties example for the STL vector::]
A vector<T>
is a Container
supports random access to elements, constant time insertion and removal of
elements at the end, and linear time insertion and removal of elements at
the beginning or in the middle. The number of elements in a vector may
vary dynamically; memory management is automatic. Vector is the simplest
of the STL container classes, and in many cases the most efficient.
[The template parameters of a class template usually must satisfy a set of requirements. Many of these can simply be expressed by listing which concept a template parameter must conform to, but some type requirements are slightly more complicated, and involve a relationship between two different template parameters. If the type is not a template and hence has no template parameters, this section can be skipped.]
Parameter | Default | Type Requirements | Description |
---|---|---|---|
T |
| None, except for those imposed by the requirements of Random Access Container and Back Insertion Sequence. | The value type. This is also defined as
|
Alloc | alloc | Alloc is an Allocator. | The set's allocator, used for all internal memory management. |
... |
[A list of the concepts that this type is a model of, and links to those concepts. Note that a type may be a model of more than one concept. If a type is a model of two different concepts, that simply means that it satisfies the requirements of both. if the type fails to model any pre-existing concept, this section may be skipped.]
[The next two sections, associated types and valid expressions, present expressions involving types that model the concept being defined. This section defines the meaning of the variables and identifiers used in those expressions.]
Symbol | Description |
---|---|
X | A type that is a model of the [Concept Name] |
x, a, b | An object of type X |
T | a type that is a model of the parameter type |
... |
[A concept is a set of requirements on some type. Frequently, however, some of those requirements involve some other type. For example, one of the Unary Function requirements is that a Unary Function must have an argument type; if F is a type that models Unary Function and f is an object of type F, then, in the expression f(x), x must be of F's argument type. If a concept does not have any such associated types this section can be eliminated.]
Type | Description |
---|---|
X::value_type | A type of an object stored in [Concept Name]. Same as T |
X::iterator | An object of type X |
... |
[A type will support all the operations of the concept which the type models. In addition, it will support all the functions which are declared in any public base classes. All these valid expressions may be listed here. The reason that this section is optional is that listing all the valid expressions implemented by base clases and other concepts can be long, tedious and redundant. On the other hand, following links back to the concepts and/or base class can also sometimes be tedious so it's hard to make a hard and fast rule]
In addition to the expressions defined in Random Access
Container and Back
Insertion Sequence the following expressions must be valid for any
object v
of type vector<T>
.
Expression | Type Requirements | Return Type | Semantics |
---|---|---|---|
v.capacity() | size_type | Number of elements for which memory has been allocated. capacity() is always greater than or equal to size(). [2] [3] | |
v.reserve(size_type n) | void | If n is less than or equal to capacity(), this call has no effect. Otherwise, it is a request for allocation of additional memory. If the request is successful, then capacity() is greater than or equal to n; otherwise, capacity() is unchanged. In either case, size() is unchanged. [2] [4] | |
... | (only for templates) |
[A code fragment involving the type.]
#include <vector>
vector<int> V;
V.insert(V.begin(), 3);
assert(V.size() == 1 && V.capacity() >= 1 && V[0] == 3);
Comment on This Page
You must be logged in to post a comment.