Difference between revisions of "cpp/container/vector"
(use lc) |
(update par) |
||
Line 25: | Line 25: | ||
===Template parameters=== | ===Template parameters=== | ||
− | {{ | + | {{par begin}} |
− | {{ | + | {{par inc | cpp/container/param list T | vector}} |
− | {{ | + | {{par inc | cpp/container/param list Allocator | vector}} |
− | {{ | + | {{par end}} |
===Specializations=== | ===Specializations=== |
Revision as of 16:13, 31 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 capacity() function. Extra memory can be returned to the system via a call to shrink_to_fit().
Reallocations are usually costly operations in terms of performance. reserve() 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.
| ||||||||||||||
Allocator | - | An allocator that is used to acquire/release memory and to construct/destroy the elements in that memory. The type must meet the requirements of Allocator. The behavior is undefined(until C++20)The program is ill-formed(since C++20) if Allocator::value_type is not the same as T .
|
Specializations
The standard library provides a specialization of std::vector
for the type bool, which is optimized for space efficiency.