C++ vectors
In C++, a vector is a sequence container that stores elements in a dynamically allocated array. Unlike arrays, vectors can grow or shrink in size dynamically at runtime. Vectors are part of the C++ Standard Library and provide a more flexible and efficient way to handle collections of data compared to traditional arrays. They are defined in the <vector> header.
Vectors are commonly used in C++ because they allow:
std::vector<T> vector_name;
T
: The type of the elements (e.g., int
, float
, std::string
).You can also define a vector with an initial size:
std::vector<T> vector_name(size);
Where size
is the number of elements initialized to default values.
#include <iostream>
#include <vector>
int main() {
// Declare a vector of integers
std::vector<int> v;
// Adding elements to the vector
v.push_back(10);
v.push_back(20);
v.push_back(30);
// Displaying elements using a range-based for loop
for (int i : v) {
std::cout << i << " ";
}
return 0;
}
Explanation:
push_back()
adds elements to the end of the vector.
#include <iostream>
#include <vector>
int main() {
// Declare a vector of integers with initial values
std::vector<int> v = {1, 2, 3, 4, 5};
// Display elements using a range-based for loop
for (int i : v) {
std::cout << i << " ";
}
return 0;
}
Explanation:
v
is initialized with values {1, 2, 3, 4, 5}
.std::vector
provides several useful member functions to work with vectors:
push_back()
Adds an element to the end of the vector.
std::vector<int> v = {1, 2, 3};
v.push_back(4); // Adds 4 to the end of the vector
size()
Returns the number of elements in the vector.
std::cout << "Size of vector: " << v.size() << std::endl;
empty()
Checks if the vector is empty.
if (v.empty()) {
std::cout << "The vector is empty!" << std::endl;
} else {
std::cout << "The vector is not empty!" << std::endl;
}
at()
Accesses an element with bounds checking.
std::cout << "Element at index 2: " << v.at(2) << std::endl;
Note: Unlike the []
operator, at()
throws an std::out_of_range
exception if the index is out of bounds.
front()
and back()
Access the first and last elements of the vector, respectively.
std::cout << "First element: " << v.front() << std::endl; // Output: 1
std::cout << "Last element: " << v.back() << std::endl; // Output: 4
pop_back()
Removes the last element from the vector.
v.pop_back(); // Removes 4 from the vector
clear()
Removes all elements from the vector, leaving it empty.
v.clear(); // The vector is now empty
resize()
Resizes the vector to a new size. If the new size is larger, new elements are initialized to their default values.
v.resize(7); // Resize the vector to 7 elements
You can iterate over a vector using different methods, such as traditional loops, range-based for loops, or iterators.
for
Loop
#include <iostream>
#include <vector>
int main() {
std::vector<int> v = {10, 20, 30, 40};
// Traditional for loop
for (size_t i = 0; i < v.size(); ++i) {
std::cout << v[i] << " ";
}
return 0;
}
Example 2: Using Range-Based for
Loop
#include <iostream>
#include <vector>
int main() {
std::vector<int> v = {1, 2, 3, 4, 5};
// Range-based for loop
for (int i : v) {
std::cout << i << " ";
}
return 0;
}
Example 3: Using Iterators
#include <iostream>
#include <vector>
int main() {
std::vector<int> v = {5, 10, 15};
// Using iterators to traverse the vector
for (auto it = v.begin(); it != v.end(); ++it) {
std::cout << *it << " ";
}
return 0;
}
std::sort
, std::find
, etc.Use vectors when: