Open In App

Advantages of Vector Over Array in C++

Last Updated : 13 Dec, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

In C++, both vectors and arrays are used to store collections of elements, but vector offers significant advantages over arrays in terms of flexibility, functionality, and ease of use. This article explores the benefits of using vectors in C++ programming.

The major advantages of vector over arrays are:

Dynamic Resizing

Unlike arrays, vectors can dynamically resize themselves. This means you don’t need to know the size of the vector in advance, it can grow and shrink according to the number of elements present in it.

C++
#include <bits/stdc++.h>
using namespace std;

int main() {
    vector<int> v;
  
  	// Initial size
  	cout << v.size() << endl;

    // Add elements dynamically
    for (int i = 1; i <= 5; ++i)
        v.push_back(i);

    // Size after inserting elements
  	cout << v.size();
    return 0;
}

Output
0
5

Built-in Functions and Operations

Vectors come with a variety of member functions, such as push_back(), pop_back(), insert(), erase(), and more, which simplify many operations. Apart from that, vectors can be easily copied from one to another using assignment operator.

C++
#include <bits/stdc++.h>
using namespace std;

int main() {
    vector<int> v = {1, 2, 3, 4, 5};

    // Remove the last element
    v.pop_back();

    // Insert a new element at the beginning
    v.insert(v.begin(), 0);

    for (auto i : v)
        cout << i << " ";
    return 0;
}

Output
0 1 2 3 4 

Memory Management

Vectors handle memory allocation and deallocation automatically, whereas arrays require manual allocation and deallocation.

C++
#include <bits/stdc++.h>
using namespace std;

int main() {
    vector<int> v = {1, 2, 3};
  
  	// No manual reallocation needed
    v.push_back(4);
  
    for (auto i : v)
        cout << i << " ";
    return 0;
}

Output
1 2 3 4 

Bounds Checking

Vector supports bounds checking in at() method and throws an out_of_range exception if the index is out of bounds, offering a safer way to access elements.

C++
#include <bits/stdc++.h>
using namespace std;

int main() {
    vector<int> v = {1, 2, 3, 4};

    try {
      
        // Attempting to access out of range index
        cout << v.at(5) << endl;
      
    } catch (const out_of_range& e) {
        cout << "Exception: " << e.what() << endl;
    }
  
    return 0;
}

Output
Exception: vector::_M_range_check: __n (which is 5) >= this->size() (which is 4)

Standard Template Library (STL) Integration

Vectors are fully compatible with STL algorithms like sort, find, remove_if, making it easier to make use of inbuilt functionality of the language.

C++
#include <bits/stdc++.h>
using namespace std;

int main() {

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

    // Sort vector
    sort(v.begin(), v.end());
  
  	// Reverse sorted vector
  	reverse(v.begin(), v.end());

    for (auto i : v)
        cout << i << " ";
    return 0;
}

Output
5 4 3 2 1 

Seamless Working with Functions

When arrays are passed to a function, a separate parameter for size is also passed whereas in case of passing a vector to a function, there is no such need as vector maintains variables which keeps track of size of container at all times. Also, it can be easily passed and returned as both value and reference.

C++
#include <bits/stdc++.h> 
using namespace std;

// Take vector as argument as reference but return
// by value
vector<int> rev(vector<int>& v) {
  	reverse(v.begin(), v.end());
  	return v;
}

int main()  { 
    vector<int> v1 = {1, 2, 3, 4, 5};
	
  	vector<int> v2 = rev(v1);
  
  	for (auto i: v2) cout << i << " ";
    return 0; 
} 

Output
5 4 3 2 1 


Next Article

Similar Reads