Difference between revisions of "cpp/container/vector"
(+template params) |
(use templates) |
||
Line 26: | Line 26: | ||
====Template parameters==== | ====Template parameters==== | ||
{{param list begin}} | {{param list begin}} | ||
− | {{param list | + | {{param list template | cpp/container/param list T | vector}} |
− | {{param list | + | {{param list template | cpp/container/param list Allocator | vector}} |
{{param list end}} | {{param list end}} | ||
Revision as of 15:56, 12 May 2013
Defined in header <vector>
|
||
template< class T, |
||
std::vector
is a sequence container that encapsulates dynamic size arrays.
The elements are stored contiguously, which means that elements can be accessed not only through iterators, but also using offsets on regular pointers to elements. This means that a pointer to an element of a vector may be passed to any function that expects a pointer to an element of an array.
The storage of the vector is handled automatically, being expanded and contracted as needed. Vectors usually occupy more space than static arrays, because more memory is allocated to handle future growth. This way a vector does not need to reallocate each time an element is inserted, but only when the additional memory is exhausted. The total amount of allocated memory can be queried using Template:rlf function. Extra memory can be returned to the system via a call to Template:rlf.
Reallocations are usually costly operations in terms of performance. Template:rlf function can be used to eliminate reallocations if the number of elements is known beforehand.
The complexity (efficiency) of common operations on vectors is as follows:
- Random access - constant O(1)
- Insertion or removal of elements at the end - amortized constant O(1)
- Insertion or removal of elements - linear in distance to the end of the vector O(n)
std::vector
meets the requirements of Template:concept, Template:concept, Template:concept and Template:concept.
Contents |
Template parameters
Specializations
The standard library provides a specialization of std::vector
for the type bool, which is optimized for space efficiency.