Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask question.(5)

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

ITtutoria

ITtutoria Logo ITtutoria Logo

ITtutoria Navigation

  • Python
  • Java
  • Reactjs
  • JavaScript
  • R
  • PySpark
  • MYSQL
  • Pandas
  • QA
  • C++
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Python
  • Science
  • Java
  • JavaScript
  • Reactjs
  • Nodejs
  • Tools
  • QA
Home/ Questions/Resolved the problem of the c++ vector subscript out of range.
Next
Answered
John Murphy
  • 21
John Murphy
Asked: May 18, 20222022-05-18T16:33:34+00:00 2022-05-18T16:33:34+00:00In: cpp

Resolved the problem of the c++ vector subscript out of range.

  • 21

. Advertisement .

..3..

. Advertisement .

..4..

I am working on cpp, but I found the following warning message:

Debug assertion failed. C++ vector subscript out of range

Is there any way to stabilize the issue “c++ vector subscript out of range”? I read a lot of topics about this, but all of them were trying to install anything. Is this the correct way, or any recommendation for me? Please find the beginning command below:

#include "stdafx.h"
 #include "iostream"
 #include "vector"
 using namespace std;
 
 int _tmain(int argc, _TCHAR * argv[])
 {
  vector<int> v;
 
  cout << "Hello India" << endl;
  cout << "Size of vector is: " << v.size() << endl;
  for (int i = 1; i <= 10; ++i)
  {
  v.push_back(i);
 
  }
  cout << "size of vector: " << v.size() << endl;
 
  for (int j = 10; j > 0; --j)
  {
  cout << v[j];
  }
 
  return 0;
 }
vector
  • 2 2 Answers
  • 46 Views
  • 0 Followers
  • 0
Answer
Share
  • Facebook
  • Report

2 Answers

  • Voted
  • Oldest
  • Recent
  • Random
  1. Best Answer
    lyytutoria Expert
    2022-06-11T23:51:45+00:00Added an answer on June 11, 2022 at 11:51 pm

    The cause:

    This error happens because arr[10] is out of range, it has not been assigned yet. No matter how you index pushbacks, your vector has 10 elements indexed within the range from 0 (0 or 1) to 9, and all other 0 elements. Therefore, your second loop contains v[j], from 1 to 9.

    Solution:

    To fix the error, you can do as below:

    for(int j = 9;j >= 0;--j)
    {
     cout << v[j];
    }

    It will be better if you think of indexes as 0-based. Therefore, we recommend you also replace your first loop by this:

    for(int i = 0;i < 10;++i)
    {
     v.push_back(i);
    }

    Moreover, iterators also can have access to the contents of a container. In this case, iterators can be used (in this example is a reverse iterator).

    for (vector<int>::reverse_iterator i = v.rbegin(); i != v.rend(); ++i)
    {
     std::cout << *i << std::endl;
    }
    • 5
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report
  2. Jules Le Roy
    2022-05-25T20:31:36+00:00Added an answer on May 25, 2022 at 8:31 pm

    v contains 10 element. The index starts at 0 to 9.

    for(int j=10;j>0;--j)
    {
     cout<<v[j]; // v[10] out of range
    }

    You should update the for loop

    for(int j=9; j>=0; --j)
    // ^^^^^^^^^^
    {
     cout<<v[j]; // out of range
    }

    You can also use reverse iterator for printing elements in reverse order

    for (auto ri = v.rbegin(); ri != v.rend(); ++ri)
    {
     std::cout << *ri << std::endl;
    }
    • 15
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

Sidebar

Ask A Question
  • How to Split String by space in C++
  • How To Convert A Pandas DataFrame Column To A List
  • How to Replace Multiple Characters in A String in Python?
  • How To Remove Special Characters From String Python

Explore

  • Home
  • Tutorial

Footer

ITtutoria

ITtutoria

This website is user friendly and will facilitate transferring knowledge. It would be useful for a self-initiated learning process.

@ ITTutoria Co Ltd.

Tutorial

  • Home
  • Python
  • Science
  • Java
  • JavaScript
  • Reactjs
  • Nodejs
  • Tools
  • QA

Legal Stuff

  • About Us
  • Terms of Use
  • Privacy Policy
  • Contact Us

DMCA.com Protection Status

Help

  • Knowledge Base
  • Support

Follow

© 2022 Ittutoria. All Rights Reserved.

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.