The STL vector
is a dynamic array that automatically resizes when elements are added or removed. Unlike std::array
, it is resizable and more flexible.
✅ Key Features:
✔ Dynamic Size – Grows/shrinks as needed.
✔ Supports Iterators – Easy traversal.
✔ Random Access – vector[i]
is fast.
✔ Built-in Functions – push_back()
, pop_back()
, sort()
, etc.
1️⃣ Syntax & Declaration
#include <iostream>
#include <vector> // Include vector library
using namespace std;
int main() {
vector<int> vec = {10, 20, 30, 40, 50}; // Declare and initialize
cout << "First Element: " << vec[0] << endl;
cout << "Last Element: " << vec.back() << endl;
return 0;
}
🔹 Output
First Element: 10
Last Element: 50
✅ Unlike std::array
, vector
size is flexible!
2️⃣ Adding & Removing Elements
🔹 Adding Elements
vector<int> vec;
vec.push_back(10); // Add 10
vec.push_back(20);
vec.push_back(30);
🔹 Removing Elements
vec.pop_back(); // Removes last element
🔹 Insert Elements
vec.insert(vec.begin() + 1, 15); // Insert 15 at index 1
🔹 Erase Elements
vec.erase(vec.begin() + 1); // Removes element at index 1
✅ vector
automatically manages memory! 🚀
3️⃣ Traversing a Vector
🔹 Using a Simple Loop
for (int i = 0; i < vec.size(); i++) {
cout << vec[i] << " ";
}
🔹 Using a Range-Based Loop
for (int val : vec) {
cout << val << " ";
}
🔹 Using Iterators
for (auto it = vec.begin(); it != vec.end(); it++) {
cout << *it << " ";
}
✅ Vectors support iterators, making traversal easy!
4️⃣ Common Vector Functions
🔹 Size & Capacity
cout << "Size: " << vec.size() << endl;
cout << "Capacity: " << vec.capacity() << endl;
🔹 Accessing Elements
cout << vec.front(); // First element
cout << vec.back(); // Last element
cout << vec.at(2); // Safe access
🔹 Checking if Empty
if (vec.empty()) cout << "Vector is empty";
🔹 Resizing Vector
vec.resize(10); // Increases size to 10
5️⃣ Sorting a Vector
#include <iostream>
#include <vector>
#include <algorithm> // Sorting functions
using namespace std;
int main() {
vector<int> vec = {50, 10, 40, 20, 30};
sort(vec.begin(), vec.end()); // Sort in ascending order
cout << "Sorted Vector: ";
for (int val : vec) cout << val << " ";
return 0;
}
🔹 Output
Sorted Vector: 10 20 30 40 50
✅ Uses sort()
function from <algorithm>
.
6️⃣ Swapping Two Vectors
#include <iostream>
#include <vector>
using namespace std;
int main() {
vector<int> vec1 = {1, 2, 3};
vector<int> vec2 = {7, 8, 9};
vec1.swap(vec2); // Swap contents
cout << "After swapping:\n";
for (int val : vec1) cout << val << " "; // 7 8 9
cout << endl;
for (int val : vec2) cout << val << " "; // 1 2 3
return 0;
}
✅ swap()
exchanges contents efficiently.
7️⃣ Clearing a Vector
vec.clear(); // Removes all elements
✅ Memory remains allocated. To free memory, use:
vector<int>().swap(vec);
8️⃣ Summary
✔ std::vector<T>
– Dynamic, flexible array.
✔ Supports push_back()
, pop_back()
, insert()
, erase()
.
✔ Random access using vec[i]
, at(i)
, front()
, back()
.
✔ Sorting via sort(vec.begin(), vec.end())
.
✔ Resizable with resize()
, clear()
, swap()
.
Would you like an STL list
next? 🚀
0 Comments