Basics
Include Header
#include <vector>
Declaration
std::vector<type> vec_name;
Insertion
vec_name.push_back(value); // Add to end
vec_name.insert(vec_name.begin() + index, value); // Insert at position
Access Element
Type val = vec_name[index]; // Direct access
Type &val = vec_name.at(index); // Bounds-checked access
Check if Empty
if (vec_name.empty()) {
// Vector is empty
}
Erase Element
vec_name.erase(vec_name.begin() + index); // Remove at index
vec_name.clear(); // Remove all elements
Iterate Through Elements
for (const auto& val : vec_name) {
std::cout << val << std::endl;
}
In reverse order:
for (auto it = vec.rbegin(); it != vec.rend(); ++it) {
std::cout << *it << " ";
}
Reverse the order of a Vector
std::reverse(vec.begin(), vec.end());
Size and Capacity
vec_name.size(); // Number of elements
vec_name.capacity(); // Allocated storage capacity
vec_name.resize(new_size); // Resize vector
Other Useful Functions
vec_name.front(); // First element
vec_name.back(); // Last element
vec_name.pop_back(); // Remove last element
Check if Element Exists
#include <algorithm>
if (std::find(vec_name.begin(), vec_name.end(), value) != vec_name.end()) {
// Element found
} else {
// Element not found
}
declaration
template<class>T, class Allocator = std::allocator<t>> class vector;
T is the type of the stored valuesAllocator is the allocator type used to manage memory
member types
| Member type | Definition | Explanation |
|---|---|---|
| value_type | T | The type of elements stored in the vector. |
| allocator_type | Allocator | The allocator used by the vector to manage memory. |
| size_type | Unsigned integer type (usually std::size_t) | Used for sizes and indices. |
| difference_type | Signed integer type (usually std::ptrdiff_t) | Used for differences between iterators. |
| reference | value_type& | Reference to an element, allows modification. |
| const_reference | const value_type& | Const reference to an element, read-only access. |
| pointer |
Allocator::pointer (until C++11) std::allocator_traits<Allocator>::pointer (since C++11) |
Pointer type to elements in the vector. |
| const_pointer |
Allocator::const_pointer (until C++11) std::allocator_traits<Allocator>::const_pointer (since C++11) |
Const pointer type to elements in the vector. |
| iterator |
LegacyRandomAccessIterator and LegacyContiguousIterator to value_type (until C++20) LegacyRandomAccessIterator, contiguous_iterator, and ConstexprIterator to value_type (since C++20) |
Iterator type that can traverse and modify elements. |
| const_iterator |
LegacyRandomAccessIterator and LegacyContiguousIterator to const value_type (until C++20) LegacyRandomAccessIterator, contiguous_iterator, and ConstexprIterator to const value_type (since C++20) |
Iterator type that can traverse elements but cannot modify them. |
| reverse_iterator | std::reverse_iterator<iterator> | Iterator that traverses elements in reverse order. |
| const_reverse_iterator | std::reverse_iterator<const_iterator> | Const iterator that traverses elements in reverse order. |
member functions
element access
-
reference at(size_type pos)Returns a reference to the element at the specified index. Throws
std::out_of_rangeif pos ≥ size(). -
reference operator[](size_type pos)Returns a reference to the element at the specified index. No bounds checking.
-
reference front()Returns a reference to the first element.
-
reference back()Returns a reference to the last element.
-
T* data()Returns a pointer to the underlying array serving as element storage.
iterators
-
iterator begin()Returns an iterator to the first element.
-
iterator end()Returns an iterator to one past the last element.
-
reverse_iterator rbegin()Returns a reverse iterator to the last element.
-
reverse_iterator rend()Returns a reverse iterator to one before the first element.
-
const_iterator cbegin() constReturns a const iterator to the first element.
-
const_iterator cend() constReturns a const iterator to one past the last element.
capacity
-
bool empty() constReturns true if the vector has no elements.
-
size_type size() constReturns the number of elements in the vector.
-
size_type max_size() constReturns the maximum number of elements the vector can hold.
-
void reserve(size_type new_cap)Increases the capacity of the vector to at least new_cap.
-
size_type capacity() constReturns the number of elements that can be held in currently allocated storage.
-
void shrink_to_fit()Reduces capacity to fit the size, if possible.
modifiers
-
void clear()Removes all elements from the vector.
-
iterator insert(iterator pos, const T& value)Inserts value before pos.
-
iterator insert(iterator pos, T&& value)Inserts value before pos using move semantics.
-
iterator insert(iterator pos, size_type count, const T& value)Inserts count copies of value before pos.
-
template<typename> iterator insert(iterator pos, InputIt first, InputIt last)Inserts elements from range [first, last) before pos.
-
iterator erase(iterator pos)Removes the element at pos.
-
iterator erase(iterator first, iterator last)Removes elements in the range [first, last).
-
void push_back(const T& value)Adds an element to the end.
-
void push_back(T&& value)Adds an element to the end using move semantics.
-
void pop_back()Removes the last element.
-
void resize(size_type count)Changes the number of elements stored.
-
void resize(size_type count, const T& value)Changes the number of elements stored and fills new elements with value.
-
void swap(vector<T, Allocator>& other)Exchanges the contents with another vector.