The C++ Standard Template Library (STL) provides the vector container for creating dynamic arrays. Unlike static arrays, vector can grow or shrink in size during runtime, making it a versatile choice for managing collections of elements.
Key Features of vector
- Defined in
<vector>header. - Dynamic sizing: Automatically resizes when elements are added or removed.
- Random access: Access elements using the subscript operator
[]. - Rich functionality: Provides a range of member functions like
push_back,pop_back, andsize.
1. Declaring a vector
You can declare a vector with any data type:
#include <iostream>
#include <vector> // Required for vector
using namespace std;
int main() {
vector<int> myVector; // Declare an empty vector of integers
return 0;
}
2. Initializing a vector
You can initialize a vector in different ways:
#include <iostream>
#include <vector>
using namespace std;
int main() {
vector<int> v1; // Empty vector
vector<int> v2(5, 10); // 5 elements, each initialized to 10
vector<int> v3 = {1, 2, 3}; // Using initializer list
vector<int> v4(v3); // Copy constructor
// Printing v2
cout << "v2: ";
for (int x : v2) {
cout << x << " ";
}
cout << endl;
return 0;
}
Output:
v2: 10 10 10 10 10
3. Adding and Removing Elements
The vector class provides functions to add or remove elements dynamically.
Adding Elements:
vector<int> v;
v.push_back(1); // Adds 1 to the end
v.push_back(2); // Adds 2 to the end
cout << "Elements: ";
for (int x : v) {
cout << x << " ";
}
Removing Elements:
v.pop_back(); // Removes the last element
cout << "\nAfter pop_back: ";
for (int x : v) {
cout << x << " ";
}
Output:
Elements: 1 2
After pop_back: 1
4. Accessing Elements
You can access elements using:
- Subscript Operator (
[]):vector<int> v = {10, 20, 30}; cout << "First element: " << v[0] << endl; at()Method:cout << "Second element: " << v.at(1) << endl;- Front and Back:
cout << "First: " << v.front() << ", Last: " << v.back() << endl;
Output:
First element: 10
Second element: 20
First: 10, Last: 30
5. Iterating Through a vector
Using a for Loop:
vector<int> v = {1, 2, 3};
for (int i = 0; i < v.size(); i++) {
cout << v[i] << " ";
}
Using a Range-Based for Loop:
for (int x : v) {
cout << x << " ";
}
Using Iterators:
for (auto it = v.begin(); it != v.end(); ++it) {
cout << *it << " ";
}
Output:
1 2 3
6. Common Operations
Check Size and Capacity:
cout << "Size: " << v.size() << ", Capacity: " << v.capacity() << endl;
Resize the Vector:
v.resize(5, 0); // Resize to 5 elements, new elements initialized to 0
Clear the Vector:
v.clear(); // Removes all elements
cout << "Size after clear: " << v.size() << endl;
Check if Empty:
if (v.empty()) {
cout << "Vector is empty.\n";
}
7. Sorting a vector
You can sort a vector using sort() from <algorithm>:
#include <algorithm> // Required for sort()
vector<int> v = {3, 1, 4, 1, 5};
sort(v.begin(), v.end()); // Sort in ascending order
cout << "Sorted vector: ";
for (int x : v) {
cout << x << " ";
}
Output:
Sorted vector: 1 1 3 4 5
8. Advanced Usage
Using a Vector of Pairs:
vector<pair<int, string>> students = {{1, "Alice"}, {2, "Bob"}};
for (const auto& student : students) {
cout << "ID: " << student.first << ", Name: " << student.second << endl;
}
Nested Vectors:
vector<vector<int>> matrix = {{1, 2}, {3, 4}, {5, 6}};
for (const auto& row : matrix) {
for (int x : row) {
cout << x << " ";
}
cout << endl;
}
Summary
vectoris a versatile and efficient container for managing dynamic arrays in C++.- It offers dynamic sizing, random access, and a rich set of functions for easy manipulation.
- Combine it with other STL components (like
sort) for powerful data manipulation.
With these basics, you can effectively use vector in a wide range of C++ applications!
