What is a Vector?
A vector is a sequence container that can hold elements of the same type. It is similar to an array, but with the added ability to resize itself as needed. Vectors manage their own memory and can grow or shrink in size during the program's execution.
How Do Vectors Work?
Dynamic Size: Vectors can grow or shrink in size as elements are added or removed. This flexibility makes them more versatile than static arrays.
Indexing: Like arrays, vectors use zero-based indexing, which means the first element is at index 0.
Accessing Elements: You can access elements in a vector using indices, just like you would with an array.
Creating and Using Vectors in C++
Here’s a simple example to create and use a vector in C++:
#include <iostream>
#include <vector>
using namespace std;
int main() {
// Create a vector of integers
vector<int> numbers;
// Add values to the vector
numbers.push_back(10);
numbers.push_back(20);
numbers.push_back(30);
numbers.push_back(40);
numbers.push_back(50);
// Access and print each element
for (int i = 0; i < numbers.size(); i++) {
cout << "Element at index " << i << " is " << numbers[i] << endl;
}
return 0;
}
Common Built-in Functions for `Vector`
Creating and Initializing Vectors:
`vector<T> v;`: Creates an empty vector `v` of type `T`.
`vector<T> v(n);`: Creates a vector `v` of type `T` with `n` elements, each initialized to the default value of `T`.
`vector<T> v(n, val);`: Creates a vector `v` of type `T` with `n` elements, each initialized to `val`.
Adding and Removing Elements:
`v.push_back(val);`: Adds an element `val` to the end of the vector `v`.
`v.pop_back();`: Removes the last element from the vector `v`.
`v.insert(pos, val);`: Inserts an element `val` at the position `pos` (iterator) in the vector `v`.
`v.erase(pos);`: Removes the element at the position `pos` (iterator) from the vector `v`.
`v.clear();`: Removes all elements from the vector `v`.
Accessing Elements:
`v[i];`: Accesses the element at index `i` in the vector `v`.
`v.at(i);`: Accesses the element at index `i` with bounds checking.
`v.front();`: Returns a reference to the first element in the vector `v`.
`v.back();`: Returns a reference to the last element in the vector `v`.
Size and Capacity:
`v.size();`: Returns the number of elements in the vector `v`.
`v.capacity();`: Returns the number of elements that the vector `v` can hold before needing to reallocate memory.
`v.empty();`: Returns `true` if the vector `v` is empty, otherwise returns `false`.
`v.resize(n);`: Resizes the vector `v` to contain `n` elements. If `n` is greater than the current size, new elements are value-initialized.
Iterators:
`v.begin();`: Returns an iterator to the first element in the vector `v`.
`v.end();`: Returns an iterator to one past the last element in the vector `v`.
Example Usage
Here's a code snippet demonstrating some of these functions:
#include <iostream>
#include <vector>
using namespace std;
int main() {
// Create a vector of integers
vector<int> numbers;
// Add elements to the vector
numbers.push_back(10);
numbers.push_back(20);
numbers.push_back(30);
// Access elements
cout << "First element: " << numbers.front() << endl;
cout << "Last element: " << numbers.back() << endl;
cout << "Element at index 1: " << numbers.at(1) << endl;
// Size and capacity
cout << "Size: " << numbers.size() << endl;
cout << "Capacity: " << numbers.capacity() << endl;
// Insert and erase elements
numbers.insert(numbers.begin() + 1, 15);
cout << "After inserting 15 at index 1: ";
for (int n : numbers) cout << n << " ";
cout << endl;
numbers.erase(numbers.begin() + 2);
cout << "After erasing element at index 2: ";
for (int n : numbers) cout << n << " ";
cout << endl;
// Resize
numbers.resize(5, 100); // Resize vector to 5 elements, new elements initialized to 100
cout << "After resizing: ";
for (int n : numbers) cout << n << " ";
cout << endl;
// Clear the vector
numbers.clear();
cout << "After clearing, size is: " << numbers.size() << endl;
return 0;
}
Output:
First element: 10
Last element: 30
Element at index 1: 20
Size: 3
Capacity: 4
After inserting 15 at index 1: 10 15 20 30
After erasing element at index 2: 10 15 30
After resizing: 10 15 30 100 100
After clearing, size is: 0