Difference between revisions of "cpp/container/vector"
(+template params) |
|||
Line 23: | Line 23: | ||
{{tt|std::vector}} meets the requirements of {{concept|Container}}, {{concept|AllocatorAwareContainer}}, {{concept|SequenceContainer}} and {{concept|ReversibleContainer}}. | {{tt|std::vector}} meets the requirements of {{concept|Container}}, {{concept|AllocatorAwareContainer}}, {{concept|SequenceContainer}} and {{concept|ReversibleContainer}}. | ||
+ | |||
+ | ====Template parameters==== | ||
+ | {{param list begin}} | ||
+ | {{param list item | T | The type of the elements. Aliased to member type {{tt|value_type}} }} | ||
+ | {{param list item | Allocator | An allocator that is used to acquire memory to store the elements. Must meet the requirements of {{concept|Allocator}}.}} | ||
+ | {{param list end}} | ||
===Specializations=== | ===Specializations=== |
Revision as of 15:22, 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
T | - | The type of the elements. Aliased to member type value_type
|
Allocator | - | An allocator that is used to acquire memory to store the elements. Must meet the requirements of Template:concept. |
Specializations
The standard library provides a specialization of std::vector
for the type bool, which is optimized for space efficiency.