This article comes from there :
http://www.cplusplus.com/reference/stl/vector/
Vector
Vectors are a kind of sequence container. As such, their elements are ordered following a strict linear sequence.
Vector containers are implemented as dynamic arrays; Just as regular arrays, vector containers have their elements stored in contiguous storage locations, which means that their elements can be accessed not only using iterators but also using offsets on regular pointers to elements.
But unlike regular arrays, storage in vectors is handled automatically, allowing it to be expanded and contracted as needed.
Vectors are good at:
- Accessing individual elements by their position index (constant time).
- Iterating over the elements in any order (linear time).
- Add and remove elements from its end (constant amortized time).
Compared to arrays, they provide almost the same performance for these tasks, plus they have the ability to be easily resized. Although, they usually consume more memory than arrays when their capacity is handled automatically (this is in order to accommodate extra storage space for future growth).
Compared to the other base standard sequence containers ( deque s and
Internally, vectors -like most containers- have a
Reallocations may be a costly operation in terms of performance, since they generally involve the entire storage space used by the vector to be copied to a new location. You can use member function
In their implementation in the C++ Standard Template Library vectors take two template parameters:
|
|
Where the template parameters have the following meanings:
- T:
Type of the elements. - Allocator:
Type of the allocator object used to define the storage allocation model. By default, the allocatorclass template for type T is used, which defines the simplest memory allocation model and is value-independent.
Member functions
-
(constructor)
-
Construct vector
(public member function)
-
(destructor)
-
Vector destructor
(public member function)
-
operator=
-
Copy vector content
(public member function )
Iterators :
-
begin
-
Return iterator to beginning
(public member type)
-
end
-
Return iterator to end
(public member function )
-
rbegin
-
Return reverse iterator to reverse beginning
(public member function)
-
rend
-
Return reverse iterator to reverse end
(public member function)
Capacity :
-
size
-
Return size
(public member function)
-
max_size
-
Return maximum size
(public member function )
-
resize
-
Change size
(public member function)
-
capacity
-
Return size of allocated storage capacity
(public member function)
-
empty
-
Test whether vector is empty
(public member function)
-
reserve
-
Request a change in capacity
(public member function)
Element access :
-
operator[]
-
Access element
(public member function)
-
at
-
Access element
(public member function)
-
front
-
Access first element
(public member function)
-
back
-
Access last element
(public member function)
Modifiers :
-
assign
-
Assign vector content
(public member function)
-
push_back
-
Add element at the end
(public member function)
-
pop_back
-
Delete last element
(public member function)
-
insert
-
Insert elements
(public member function)
-
erase
-
Erase elements
(public member function )
-
swap
-
Swap content
(public member function )
-
clear
-
Clear content
(public member function)
Allocator :
-
get_allocator
-
Get allocator
(public member function )
Member types
ofmember type | definition |
---|---|
reference | Allocator::reference |
const_reference | Allocator::const_reference |
iterator | Random access iterator |
const_iterator | Constant random access iterator |
size_type | Unsigned integral type |
difference_type | Signed integral type |
value_type | T |
allocator_type | Allocator |
pointer | Allocator::pointer |
const_pointer | Allocator::const_pointer |
reverse_iterator | reverse_iterator<iterator> |
const_reverse_iterator | reverse_iterator<const_iterator> |
Vector specialization: vector<bool>
The vector class template has a special template specialization for the This specialization is provided to optimize for space allocation: In this template specialization, each element occupies only one bit (which is eight times less than the smallest type in C++:
The references to elements of a
|
|
For a similar container class to contain bits, but with a fixed size, see