Vector Flashcards

1
Q

Element access

at( )

A

access specified element with bounds checking
Returns a reference to the element at specified location pos, with bounds checking.

If pos is not within the range of the container, an exception of type std::out_of_range is thrown.

Parameters
pos	-	position of the element to return
Return value
Reference to the requested element.
Exceptions
std::out_of_range if !(pos < size()).

Complexity
Constant.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Element access

operator[]

A

access specified element
Returns a reference to the element at specified location pos. No bounds checking is performed.

Parameters
pos - position of the element to return
Return value
Reference to the requested element.

Complexity
Constant.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Element access

front( )

A

access the first element
Returns a reference to the first element in the container.

Calling front on an empty container is undefined.

Parameters
(none)

Return value
reference to the first element

Complexity
Constant

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Element access

back( )

A

access the last element
Returns a reference to the last element in the container.

Calling back on an empty container causes undefined behavior.

Parameters
(none)

Return value
Reference to the last element.

Complexity
Constant.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Element access

data( )

A

direct access to the underlying array
Returns pointer to the underlying array serving as element storage. The pointer is such that range [data(); data() + size()) is always a valid range, even if the container is empty (data() is not dereferenceable in that case).

Parameters
(none)

Return value
Pointer to the underlying element storage. For non-empty containers, the returned pointer compares equal to the address of the first element.

Complexity
Constant.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Iterators
begin
cbegin( )

A

returns an iterator to the beginning
Returns an iterator to the first element of the vector.

If the vector is empty, the returned iterator will be equal to end().

range-begin-end.svg

Parameters
(none)

Return value
Iterator to the first element.

Complexity
Constant.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Iterators
end
cend( )

A

returns an iterator to the end
Returns an iterator to the element following the last element of the vector.

This element acts as a placeholder; attempting to access it results in undefined behavior.

range-begin-end.svg

Parameters
(none)

Return value
Iterator to the element following the last element.

Complexity
Constant.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Iterators
rbegin
crbegin( )

A

returns a reverse iterator to the beginning
Returns a reverse iterator to the first element of the reversed vector. It corresponds to the last element of the non-reversed vector. If the vector is empty, the returned iterator is equal to rend().

range-rbegin-rend.svg

Parameters
(none)

Return value
Reverse iterator to the first element.

Complexity
Constant.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Iterators
rend
crend( )

A

returns a reverse iterator to the end
Returns a reverse iterator to the element following the last element of the reversed vector. It corresponds to the element preceding the first element of the non-reversed vector. This element acts as a placeholder, attempting to access it results in undefined behavior.

range-rbegin-rend.svg

Parameters
(none)

Return value
Reverse iterator to the element following the last element.

Complexity
Constant.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Capacity

empty( )

A

checks whether the container is empty
hecks if the container has no elements, i.e. whether begin() == end().

Parameters
(none)

Return value
true if the container is empty, false otherwise

Complexity
Constant.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Capacity

size( )

A

returns the number of elements
Returns the number of elements in the container, i.e. std::distance(begin(), end()).

Parameters
(none)

Return value
The number of elements in the container.

Complexity
Constant.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Capacity

max_size( )

A

returns the maximum possible number of elements
Returns the maximum number of elements the container is able to hold due to system or library implementation limitations, i.e. std::distance(begin(), end()) for the largest container.

Parameters
(none)

Return value
Maximum number of elements.

Complexity
Constant.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Capacity

reserve( )

A

reserves storage
Increase the capacity of the vector to a value that’s greater or equal to new_cap. If new_cap is greater than the current capacity(), new storage is allocated, otherwise the method does nothing.

reserve() does not change the size of the vector.

If new_cap is greater than capacity(), all iterators, including the past-the-end iterator, and all references to the elements are invalidated. Otherwise, no iterators or references are invalidated.

Parameters
new_cap - new capacity of the vector
Type requirements
-T must meet the requirements of MoveInsertable.
Return value
(none)
Exceptions
std::length_error if new_cap > max_size().
any exception thrown by Allocator::allocate() (typically std::bad_alloc)
If an exception is thrown, this function has no effect (strong exception guarantee).

If T’s move constructor is not noexcept and T is not CopyInsertable into *this, vector will use the throwing move constructor. If it throws, the guarantee is waived and the effects are unspecified.

(since C++11)
Complexity
At most linear in the size() of the container.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Capacity

capacity( )

A

returns the number of elements that can be held in currently allocated storage
Returns the number of elements that the container has currently allocated space for.

Parameters
(none)

Return value
Capacity of the currently allocated storage.

Complexity
Constant.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

Capacity

shrink_to_fit( )

A

reduces memory usage by freeing unused memory
Requests the removal of unused capacity.

It is a non-binding request to reduce capacity() to size(). It depends on the implementation whether the request is fulfilled.

If reallocation occurs, all iterators, including the past the end iterator, and all references to the elements are invalidated. If no reallocation takes place, no iterators or references are invalidated.

Parameters
(none)

Type requirements
-T must meet the requirements of MoveInsertable.
Return value
(none)

Complexity
At most linear in the size of the container.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

Modifiers

clear( )

A

clears the contents
Erases all elements from the container. After this call, size() returns zero.

Invalidates any references, pointers, or iterators referring to contained elements. Any past-the-end iterators are also invalidated.

Leaves the capacity() of the vector unchanged (note: the standard’s restriction on the changes to capacity is in the specification of vector::reserve, see [1])

Parameters
(none)

Return value
(none)

Complexity
Linear in the size of the container, i.e., the number of elements.

17
Q

Modifiers

insert( )

A

inserts elements
Return value
1-2) Iterator pointing to the inserted value
3) Iterator pointing to the first element inserted, or pos if count==0.
4) Iterator pointing to the first element inserted, or pos if first==last.
5) Iterator pointing to the first element inserted, or pos if ilist is empty.
Complexity
1-2) Constant plus linear in the distance between pos and end of the container.
3) Linear in count plus linear in the distance between pos and end of the container.
4) Linear in std::distance(first, last) plus linear in the distance between pos and end of the container.
5) Linear in ilist.size() plus linear in the distance between pos and end of the container.
Exceptions
If an exception is thrown when inserting a single element at the end, and T is CopyInsertable or std::is_nothrow_move_constructible::value is true, there are no effects (strong exception guarantee).

18
Q

Modifiers

emplace( )

A

constructs element in-place
Parameters
pos - iterator before which the new element will be constructed
args - arguments to forward to the constructor of the element
Type requirements
-T (the container’s element type) must meet the requirements of MoveAssignable, MoveInsertable and EmplaceConstructible.
Return value
Iterator pointing to the emplaced element.

Complexity
Linear in the distance between pos and end of the container.

19
Q

Modifiers

erase( )

A

erases elements
Parameters
pos - iterator to the element to remove
first, last - range of elements to remove
Type requirements
-T must meet the requirements of MoveAssignable.
Return value
Iterator following the last removed element.

If pos refers to the last element, then the end() iterator is returned.

If last==end() prior to removal, then the updated end() iterator is returned.

If [first, last) is an empty range, then last is returned.
Exceptions
Does not throw unless an exception is thrown by the assignment operator of T.

Complexity
Linear: the number of calls to the destructor of T is the same as the number of elements erased, the assignment operator of T is called the number of times equal to the number of elements in the vector after the erased elements

20
Q

Modifiers

push_back( )

A

adds an element to the end
Parameters
value - the value of the element to append
Type requirements
-T must meet the requirements of CopyInsertable in order to use overload (1).
-T must meet the requirements of MoveInsertable in order to use overload (2).
Return value
(none)

Complexity
Amortized constant.

21
Q

Modifiers

emplace_back( )

A

constructs an element in-place at the end
Parameters
args - arguments to forward to the constructor of the element
Type requirements
-T (the container’s element type) must meet the requirements of MoveInsertable and EmplaceConstructible.
Return value
(none)

(until C++17)
A reference to the inserted element.

(since C++17)
Complexity
Amortized constant.

22
Q

Modifiers

pop_back( )

A

removes the last element
Parameters
(none)

Return value
(none)

Complexity
Constant.

23
Q

Modifiers

resize( )

A

changes the number of elements stored
Parameters
count - new size of the container
value - the value to initialize the new elements with
Type requirements
-T must meet the requirements of MoveInsertable and DefaultInsertable in order to use overload (1).
-T must meet the requirements of CopyInsertable in order to use overload (2).
Return value
(none)
Complexity
Linear in the difference between the current size and count. Additional complexity possible due to reallocation if capacity is less than count

24
Q

Modifiers

swap( )

A

swaps the contents
Exchanges the contents of the container with those of other. Does not invoke any move, copy, or swap operations on individual elements.

All iterators and references remain valid. The past-the-end iterator is invalidated.

If std::allocator_traits::propagate_on_container_swap::value is true, then the allocators are exchanged using an unqualified call to non-member swap. Otherwise, they are not swapped (and if get_allocator() != other.get_allocator(), the behavior is undefined).

(since C++11)
Parameters
other	-	container to exchange the contents with
Return value
(none)

Exceptions
(none)

(until C++17)
noexcept specification:
noexcept(std::allocator_traits::propagate_on_container_swap::value
|| std::allocator_traits::is_always_equal::value)
(since C++17)
Complexity
Constant.