๐ฆ C++ Vectors โ Dynamic Arrays with Power and Flexibility
๐งฒ Introduction โ Why Use std::vector
in C++
When it comes to storing a dynamic list of elements in C++, the most versatile and widely used container is std::vector
. Unlike arrays, vectors automatically resize, support random access, and are fully compatible with STL algorithms and iterators.
๐ฏ In this guide, youโll learn:
- How to declare, initialize, and manipulate vectors
- Key vector methods (
push_back()
,size()
,insert()
, etc.) - Use of iterators and algorithms with vectors
- Best practices and real-world use cases
๐ What Is a C++ Vector?
A vector
in C++ is a sequence container that stores elements contiguously in memory and allows fast access by index. It’s like a dynamic array that can grow or shrink at runtime.
Include it with:
#include <vector>
Declare a vector:
vector<int> nums;
๐ป Code Examples โ With Output
โ Declare and Add Elements
#include <iostream>
#include <vector>
using namespace std;
int main() {
vector<int> v;
v.push_back(10);
v.push_back(20);
v.push_back(30);
for (int x : v) cout << x << " ";
return 0;
}
๐ข Output:
10 20 30
โ Initialize a Vector
vector<int> v1 = {1, 2, 3};
vector<int> v2(5, 100); // 5 elements with value 100
โ Access Elements
cout << v[0]; // No bounds check
cout << v.at(1); // With bounds checking
โ Vector Operations
Function | Description |
---|---|
push_back() | Adds an element at the end |
pop_back() | Removes the last element |
size() | Returns the number of elements |
clear() | Removes all elements |
insert() | Inserts element at given position |
erase() | Removes element from position/range |
resize(n) | Resizes vector to contain n elements |
empty() | Checks if vector is empty |
๐ Using Iterators with Vectors
vector<int>::iterator it;
for (it = v.begin(); it != v.end(); ++it)
cout << *it << " ";
๐ STL Algorithms with Vectors
#include <algorithm>
sort(v.begin(), v.end());
reverse(v.begin(), v.end());
find(v.begin(), v.end(), 20);
๐ก Best Practices & Tips
๐ Use .reserve(n)
if you know the approximate size in advance
๐ก Use emplace_back()
over push_back()
for performance with objects
โ ๏ธ Avoid frequent insertion/removal at the beginningโvectors are optimized for end operations
๐ฆ Prefer vector
over arrays for safety, size flexibility, and STL compatibility
๐ ๏ธ Use Cases for Vectors
๐ Dynamic Lists โ Flexible array-like behavior
๐ฎ Game Inventories โ Add/remove items efficiently
๐ File Parsers โ Store lines or tokens dynamically
๐ Data Processing โ Use with algorithms like sort()
, accumulate()
๐งฎ Math Models โ Represent points, matrices, or time series data
๐ Summary โ Recap & Next Steps
๐ Key Takeaways:
std::vector
is a dynamic, contiguous container that supports resizing and random access- STL-compatible: works with algorithms and iterators
- Provides a wide range of member functions for flexibility
โ๏ธ Real-World Relevance:
Vectors are used in system software, simulations, data analysis, compilers, GUIs, and more due to their versatility and performance.
โ
Next Steps:
Learn about C++ Lists and understand when linked lists are better than dynamic arrays.
โFAQ โ C++ Vectors
โ How is vector different from array?
โ
Vectors resize dynamically, arrays are fixed in size.
โ Is vector faster than list?
โ
For random access and sequential traversal, yes. But lists are better for frequent insertions/removals in the middle.
โ Can I use STL algorithms with vectors?
โ
Absolutely. Vectors fully support iterators.
โ What happens if I access out of bounds?
โ ๏ธ v[i]
causes undefined behavior, while v.at(i)
throws an exception.
โ Are vectors thread-safe?
โ No. Use external synchronization for concurrent access.
Share Now :