๐Ÿงฉ C++ STL & Data Structures
Estimated reading: 3 minutes 27 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 :

Leave a Reply

Your email address will not be published. Required fields are marked *

Share

C++ Vectors

Or Copy Link

CONTENTS
Scroll to Top