Vectors

Vectors in C++ are dynamic arrays provided by the Standard Template Library (STL). Unlike normal arrays, vectors can automatically grow or shrink in size during program execution.

What is a Vector?

A vector is a sequence container that stores elements like an array but provides dynamic resizing and many useful built-in functions.

Why Use Vectors?

Vectors are useful because they:

  • Automatically manage memory
  • Resize dynamically
  • Are easier to use than arrays
  • Provide built-in STL functions
  • Improve programming efficiency

Vector Header File

To use vectors in C++, include the <vector> library.

#include <vector>

Declaring a Vector

vector<int> numbers;

This creates an empty vector of integers.

Initializing a Vector

vector<int> numbers = {10, 20, 30, 40};

Adding Elements Using push_back()

push_back() adds elements at the end of the vector.

Example

numbers.push_back(50);

Accessing Vector Elements

Elements can be accessed using indexing.

Example

cout << numbers[0];
cout << numbers.at(1);

Example Program of Vector

#include <iostream>
#include <vector>
using namespace std;

int main() {

vector<int> numbers = {1, 2, 3};

numbers.push_back(4);

for (int i = 0; i < numbers.size(); i++) {

cout << numbers[i] << endl;
}

return 0;
}

Output

1
2
3
4

Common Vector Functions

FunctionPurpose
push_back()Add element at end
pop_back()Remove last element
size()Get number of elements
clear()Remove all elements
empty()Check if vector is empty
at()Access element safely

Example of Vector Functions

#include <iostream>
#include <vector>
using namespace std;

int main() {

vector<int> numbers = {10, 20, 30};

numbers.pop_back();

cout << numbers.size();

return 0;
}

Output

2

Using Range-Based Loop with Vector

Modern C++ supports range-based loops.

Example

#include <iostream>
#include <vector>
using namespace std;

int main() {

vector<int> numbers = {5, 10, 15};

for (int num : numbers) {

cout << num << endl;
}

return 0;
}

Output

5
10
15

Vector Iterators

Vectors support iterators for traversal.

Example

#include <iostream>
#include <vector>
using namespace std;

int main() {

vector<int> v = {1, 2, 3};

for (auto it = v.begin(); it != v.end(); it++) {

cout << *it << endl;
}

return 0;
}

Difference Between Arrays and Vectors

ArraysVectors
Fixed sizeDynamic size
Manual memory handlingAutomatic memory management
Limited functionsMany built-in functions

Important Points About Vectors

  • Elements are stored in contiguous memory
  • Vectors resize automatically
  • Access is fast using indexing
  • Inserting at end is efficient

Advantages of Vectors

  • Dynamic resizing
  • Easy memory management
  • STL support
  • Flexible operations
  • Better usability than arrays

Common Mistakes with Vectors

  • Accessing invalid index
  • Forgetting vector size checks
  • Using large unnecessary copies
  • Confusing size() with capacity

Real-Life Example

Think of a shopping cart:

  • Items can be added anytime
  • Items can be removed anytime
  • Cart size changes dynamically

This is similar to how vectors work.

Applications of Vectors

Vectors are widely used in:

  • Data storage
  • Game development
  • Competitive programming
  • Database applications
  • Dynamic data processing

Why Vectors are Important

Vectors are important because they:

  • Simplify array handling
  • Improve memory management
  • Increase programming efficiency
  • Support dynamic applications

Conclusion

Vectors in C++ are powerful dynamic containers that automatically manage memory and resize during runtime. With built-in STL functions and flexible operations, vectors are widely used in modern C++ programming for efficient and scalable applications.

Home » Professional C++ > STL (Standard Template Library) > Vectors