Difference between revisions of "cpp/container/vector"
(rvv) |
|||
Line 1: | Line 1: | ||
{{cpp/title|vector}} | {{cpp/title|vector}} | ||
{{cpp/container/vector/navbar}} | {{cpp/container/vector/navbar}} | ||
− | {{ | + | {{ddcl | header=vector | |
− | + | ||
− | + | ||
template< | template< | ||
class T, | class T, | ||
− | class Allocator = std::allocator<T> | + | class Allocator {{=}} std::allocator<T> |
> class vector; | > class vector; | ||
− | + | }} | |
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | {{tt|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 | + | 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 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 {{lc|capacity()}} function. Extra memory can be returned to the system via a call to {{lc|shrink_to_fit()}} | + | 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 {{lc|capacity()}} function. Extra memory can be returned to the system via a call to {{lc|shrink_to_fit()}}{{mark c++11}}. |
− | Reallocations are usually costly operations in terms of performance. | + | Reallocations are usually costly operations in terms of performance. {{lc|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: | The complexity (efficiency) of common operations on vectors is as follows: | ||
− | * Random access - constant {{math| | + | * Random access - constant {{math|O(1)}} |
− | * Insertion or removal of elements at the end - amortized constant {{math| | + | * Insertion or removal of elements at the end - amortized constant {{math|O(1)}} |
− | * Insertion or removal of elements - linear in | + | * Insertion or removal of elements - linear in distance to the end of the vector {{math|O(n)}} |
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
===Template parameters=== | ===Template parameters=== | ||
{{par begin}} | {{par begin}} | ||
− | {{par inc|cpp/container/param list T|vector}} | + | {{par inc | cpp/container/param list T | vector}} |
− | {{par inc|cpp/container/param list Allocator|vector}} | + | {{par inc | cpp/container/param list Allocator | vector}} |
{{par end}} | {{par end}} | ||
===Specializations=== | ===Specializations=== | ||
− | The standard library provides a specialization of {{tt|std::vector}} for the type {{c | + | The standard library provides a specialization of {{tt|std::vector}} for the type {{c|bool}}, which is optimized for space efficiency. |
{{dsc begin}} | {{dsc begin}} | ||
− | {{dsc inc|cpp/container/dsc | + | {{dsc inc | cpp/container/dsc vector_bool}} |
{{dsc end}} | {{dsc end}} | ||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
===Member types=== | ===Member types=== | ||
{{dsc begin}} | {{dsc begin}} | ||
− | {{dsc hitem|Member type|Definition}} | + | {{dsc hitem | Member type | Definition}} |
− | {{dsc inc|cpp/container/dsc value_type|vector}} | + | {{dsc inc | cpp/container/dsc value_type | vector}} |
− | {{dsc inc|cpp/container/dsc allocator_type|vector}} | + | {{dsc inc | cpp/container/dsc allocator_type | vector}} |
− | {{dsc inc|cpp/container/dsc size_type|vector}} | + | {{dsc inc | cpp/container/dsc size_type | vector}} |
− | {{dsc inc|cpp/container/dsc difference_type|vector}} | + | {{dsc inc | cpp/container/dsc difference_type | vector}} |
− | {{dsc inc|cpp/container/dsc reference|vector}} | + | {{dsc inc | cpp/container/dsc reference | vector}} |
− | {{dsc inc|cpp/container/dsc const_reference|vector}} | + | {{dsc inc | cpp/container/dsc const_reference | vector}} |
− | {{dsc inc|cpp/container/dsc pointer|vector}} | + | {{dsc inc | cpp/container/dsc pointer | vector}} |
− | {{dsc inc|cpp/container/dsc const_pointer|vector}} | + | {{dsc inc | cpp/container/dsc const_pointer | vector}} |
− | {{dsc inc|cpp/container/dsc iterator|vector}} | + | {{dsc inc | cpp/container/dsc iterator | vector}} |
− | {{dsc inc|cpp/container/dsc const_iterator|vector}} | + | {{dsc inc | cpp/container/dsc const_iterator | vector}} |
− | {{dsc inc|cpp/container/dsc reverse_iterator|vector}} | + | {{dsc inc | cpp/container/dsc reverse_iterator | vector}} |
− | {{dsc inc|cpp/container/dsc const_reverse_iterator|vector}} | + | {{dsc inc | cpp/container/dsc const_reverse_iterator | vector}} |
{{dsc end}} | {{dsc end}} | ||
===Member functions=== | ===Member functions=== | ||
{{dsc begin}} | {{dsc begin}} | ||
− | {{dsc inc|cpp/container/dsc constructor|vector}} | + | {{dsc inc | cpp/container/dsc constructor | vector}} |
− | {{dsc inc|cpp/container/dsc destructor|vector}} | + | {{dsc inc | cpp/container/dsc destructor | vector}} |
− | {{dsc inc|cpp/container/dsc operator{{=}}|vector}} | + | {{dsc inc | cpp/container/dsc operator{{=}} | vector}} |
− | {{dsc inc|cpp/container/dsc assign | + | {{dsc inc | cpp/container/dsc assign | vector}} |
− | + | {{dsc inc | cpp/container/dsc get_allocator | vector}} | |
− | {{dsc inc|cpp/container/dsc get_allocator|vector}} | + | |
− | {{dsc h2|Element access}} | + | {{dsc h2 | Element access}} |
− | {{dsc inc|cpp/container/dsc at|vector}} | + | {{dsc inc | cpp/container/dsc at | vector}} |
− | {{dsc inc|cpp/container/dsc operator_at|vector}} | + | {{dsc inc | cpp/container/dsc operator_at | vector}} |
− | {{dsc inc|cpp/container/dsc front|vector}} | + | {{dsc inc | cpp/container/dsc front | vector}} |
− | {{dsc inc|cpp/container/dsc back|vector}} | + | {{dsc inc | cpp/container/dsc back | vector}} |
− | {{dsc inc|cpp/container/dsc data|vector}} | + | {{dsc inc | cpp/container/dsc data | vector}} |
− | {{dsc h2|Iterators}} | + | {{dsc h2 | Iterators}} |
− | {{dsc inc|cpp/container/dsc begin|vector}} | + | {{dsc inc | cpp/container/dsc begin | vector}} |
− | {{dsc inc|cpp/container/dsc end|vector}} | + | {{dsc inc | cpp/container/dsc end | vector}} |
− | {{dsc inc|cpp/container/dsc rbegin|vector}} | + | {{dsc inc | cpp/container/dsc rbegin | vector}} |
− | {{dsc inc|cpp/container/dsc rend|vector}} | + | {{dsc inc | cpp/container/dsc rend | vector}} |
− | {{dsc h2|Capacity}} | + | {{dsc h2 | Capacity}} |
− | {{dsc inc|cpp/container/dsc empty|vector}} | + | {{dsc inc | cpp/container/dsc empty | vector}} |
− | {{dsc inc|cpp/container/dsc size|vector}} | + | {{dsc inc | cpp/container/dsc size | vector}} |
− | {{dsc inc|cpp/container/dsc max_size|vector}} | + | {{dsc inc | cpp/container/dsc max_size | vector}} |
− | {{dsc inc|cpp/container/dsc reserve|vector}} | + | {{dsc inc | cpp/container/dsc reserve | vector}} |
− | {{dsc inc|cpp/container/dsc capacity|vector}} | + | {{dsc inc | cpp/container/dsc capacity | vector}} |
− | {{dsc inc|cpp/container/dsc shrink_to_fit|vector}} | + | {{dsc inc | cpp/container/dsc shrink_to_fit | vector}} |
− | {{dsc h2|Modifiers}} | + | {{dsc h2 | Modifiers}} |
− | {{dsc inc|cpp/container/dsc clear|vector}} | + | {{dsc inc | cpp/container/dsc clear | vector}} |
− | {{dsc inc|cpp/container/dsc insert | + | {{dsc inc | cpp/container/dsc insert | vector}} |
− | + | {{dsc inc | cpp/container/dsc emplace | vector}} | |
− | {{dsc inc|cpp/container/dsc emplace|vector}} | + | {{dsc inc | cpp/container/dsc erase | vector}} |
− | {{dsc inc|cpp/container/dsc erase|vector}} | + | {{dsc inc | cpp/container/dsc push_back | vector}} |
− | {{dsc inc|cpp/container/dsc push_back|vector}} | + | {{dsc inc | cpp/container/dsc emplace_back | vector}} |
− | {{dsc inc|cpp/container/dsc emplace_back | + | {{dsc inc | cpp/container/dsc pop_back | vector}} |
− | + | {{dsc inc | cpp/container/dsc resize | vector}} | |
− | {{dsc inc|cpp/container/dsc pop_back|vector}} | + | {{dsc inc | cpp/container/dsc swap | vector}} |
− | {{dsc inc|cpp/container/dsc resize|vector}} | + | |
− | {{dsc inc|cpp/container/dsc swap|vector}} | + | |
{{dsc end}} | {{dsc end}} | ||
===Non-member functions=== | ===Non-member functions=== | ||
{{dsc begin}} | {{dsc begin}} | ||
− | {{dsc inc|cpp/container/dsc operator_cmp|vector}} | + | {{dsc inc | cpp/container/dsc operator_cmp | vector}} |
− | {{dsc inc|cpp/container/dsc swap2 | + | {{dsc inc | cpp/container/dsc swap2 | vector}} |
− | + | ||
{{dsc end}} | {{dsc end}} | ||
− | + | [[de:cpp/container/vector]] | |
− | + | [[es:cpp/container/vector]] | |
− | + | [[fr:cpp/container/vector]] | |
− | + | [[it:cpp/container/vector]] | |
− | + | [[ja:cpp/container/vector]] | |
− | + | [[ko:cpp/container/vector]] | |
− | + | [[pt:cpp/container/vector]] | |
− | + | [[ru:cpp/container/vector]] | |
− | + | [[zh:cpp/container/vector]] | |
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + |
Revision as of 14:53, 12 September 2023
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()(C++11).
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)
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.
space-efficient dynamic bitset (class template specialization) |
Member types
Member type | Definition | ||||
value_type
|
T
| ||||
allocator_type
|
Allocator
| ||||
size_type
|
Unsigned integer type (usually std::size_t) | ||||
difference_type
|
Signed integer type (usually std::ptrdiff_t) | ||||
reference
|
value_type& | ||||
const_reference
|
const value_type& | ||||
pointer
|
| ||||
const_pointer
|
| ||||
iterator
|
| ||||
const_iterator
|
| ||||
reverse_iterator
|
std::reverse_iterator<iterator> | ||||
const_reverse_iterator
|
std::reverse_iterator<const_iterator> |
Member functions
constructs the vector (public member function) | |
destructs the vector (public member function) | |
assigns values to the container (public member function) | |
assigns values to the container (public member function) | |
returns the associated allocator (public member function) | |
Element access | |
access specified element with bounds checking (public member function) | |
access specified element (public member function) | |
access the first element (public member function) | |
access the last element (public member function) | |
direct access to the underlying contiguous storage (public member function) | |
Iterators | |
(C++11) |
returns an iterator to the beginning (public member function) |
(C++11) |
returns an iterator to the end (public member function) |
(C++11) |
returns a reverse iterator to the beginning (public member function) |
(C++11) |
returns a reverse iterator to the end (public member function) |
Capacity | |
checks whether the container is empty (public member function) | |
returns the number of elements (public member function) | |
returns the maximum possible number of elements (public member function) | |
reserves storage (public member function) | |
returns the number of elements that can be held in currently allocated storage (public member function) | |
(DR*) |
reduces memory usage by freeing unused memory (public member function) |
Modifiers | |
clears the contents (public member function) | |
inserts elements (public member function) | |
(C++11) |
constructs element in-place (public member function) |
erases elements (public member function) | |
adds an element to the end (public member function) | |
(C++11) |
constructs an element in-place at the end (public member function) |
removes the last element (public member function) | |
changes the number of elements stored (public member function) | |
swaps the contents (public member function) |
Non-member functions
(removed in C++20)(removed in C++20)(removed in C++20)(removed in C++20)(removed in C++20)(C++20) |
lexicographically compares the values of two vector s (function template) |
specializes the std::swap algorithm (function template) |