🧩 C++ STL & Data Structures
Estimated reading: 3 minutes 271 views

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

FunctionDescription
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 :
Share

C++ Vectors

Or Copy Link

CONTENTS
Scroll to Top