Vector subscript out of range in C++ – Solution

Prerequisites:

Introduction:

Vectors are used to store similar data types of elements dynamically, thereby known as dynamic arrays. Vectors can be iterated using indexing from size 0 to N – 1 where N = vector’s size or using the auto keyword (similar to fresh loop).

 Syntax:

  1. for (int i = 0; i < vec.size(); i++) { cout<<vec[i]<<endl; }
  2. for (auto it : vec) { cout<<*it<<endl; }

Note: size() gives the number of elements of the vector.

Issue:

While iterating, we might update/ delete/ modify/ resize the vector’s size, which results in indexing and the number of elements. So, in some cases, your code works fine, although you have any one of the above operations. Most of the codes give subscript out of range, irrespective of compiler/IDE you are using. In this article, you are going to see 2 scenarios where you could get some insights on how to solve vector subscript out of range in C++.

Examples:

Example 1 =>

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

int main()
{
    vector<int> v;
    for (int i = 1; i <= 10; ++i)
        v.push_back(i);

    for (int j = 10; j > 0; --j)
        cout << v.at(j) << " ";
    return 0;
}

Explanation: In the above code, we insert only 10 elements indexing from 0 to 9 but accessing the 10th indexed number in the second for loop. So, this is one of the reasons regarding the indexing issue.

Resolve subscript out of range

Solution: Try to find the indexing when you are iterating over the vectors and check their sizes after every operation on them. You can also iterators (*it) for accessing them.

Example 2 =>

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

int main()
{
    vector<int> v;
    for (int i = 1; i <= 10; ++i)
        v.push_back(i);

    for (int j=0; j<=10; j++){
        cout << v.at(j) << " k , ";
        if(j==8)
            v.erase(v.begin()+8);
    }
    return 0;
}

Explanation: In the above code, the vector is of size 10. We have made erase operation on vector if j==8, thereby resulting in the vector size as 9. But the iteration goes to 10 and outputs the “subscript out-of-range” exception.

Solution: Must be cautious while performing operations on vector and use debugging tools whenever required.

Points to be noted:

  1. Since “std::vector::operator[]” doesn’t check for limits, it won’t throw an exception if an improper index is passed in. Using operator[] to access an index that is out of bounds results in undefined behavior.
  2. If you wish to throw an exception, use std::vector::at() to check the boundaries. If an invalid index is passed in, it will throw a std::out of range exception.
  3. Ex: If there are fewer than two elements in the index, the index[2] activates undefined behavior, which means the compiler can do whatever it wants. It is not necessary to throw an exception in this case. If you want an exception, use index.at(2), which is designed or intended.

Leave a Reply

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