You are using a version without Ads of this website. Please, consider donating:

### vector subscript out of range

I've been using visual C++ Express to work through some elementary exercises...

After I receive the message "vector subscript out of range", I am given the option to "abort", "retry", or "ignore" the message.

I tried "retrying" because I thought it would help me debug, but all I did was get really confused and become afraid my standard libraries were in the wrong place.

However.... I think that it's as simple of a thing as having a vector subscript out of range. However.... I can't find the problem. They all start at zero, and I didn't know there was an upper end of a subscript range?

 ``12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758`` ``````// A program to count the number of times each distinct word is used in an input #include "chapter3.h" int main() { // Read the data cout << "Enter the words, followed by Ctrl + Z:"; string x; vector words; // invariant: words contains all the entered words while (cin>>x) { words.push_back(x); } // sort the data sort(words.begin(),words.end()); // create vectors for unique words and a count variable vector distinct; int count = 0; vector distinctCount; // test for word distinctness and store values in new vectors typedef vector::size_type vec_sz; vec_sz sizeWords = words.size(); int i = 0; // invariant: there are i distinct words in distinct while (i != sizeWords) { distinct[count] = words[i]; int temp = 1; while (words[i] == distinct[count]) { distinctCount[count] = temp; ++temp; ++i; } ++count; } // output the data for (int z = 0; z != count; ++z) { cout << distinct[z] << ": " << distinctCount[z] << endl; } // hold the execution window open cin.clear(); cin.sync(); cin.ignore(); return 0; }``````

Any help finding my out of range vector subscripts would be much appreciated.
I have not included the header file, but it's a pretty standard beginner header file.
> and I didn't know there was an upper end of a subscript range?

There is. The inclusive range is from zero up to the size of the vector minus one.

These vectors are created empty ( size() == 0 )
 ``123`` ``````vector distinct; vector distinctCount;``````

To add items to them, you need to increase the size of the vector; for instance with `push_back()`
OK. I've rewritten the code to use "push_back", but I'm still getting "vector subscript out of range" error.

 ``12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758`` ``````// A program to count the number of times each distinct word is used in an input #include "chapter3.h" int main() { // Read the data cout << "Enter the words, followed by Ctrl + Z:"; string x; vector words; // invariant: words contains all the entered words while (cin>>x) { words.push_back(x); } // sort the data sort(words.begin(),words.end()); // create vectors for unique words and a count variable vector distinct; int count = 0; vector distinctCount; // test for word distinctness and store values in new vectors typedef vector::size_type vec_sz; vec_sz sizeWords = words.size(); int i = 0; // invariant: there are i distinct words in distinct while (i != sizeWords) { distinct.push_back(words[i]); int temp = 1; while (words[i] == distinct[count]) { distinctCount.push_back(temp); ++temp; ++i; } ++count; } // output the data for (int z = 0; z != count; ++z) { cout << distinct[z] << ": " << distinctCount[z] << endl; } // hold the execution window open cin.clear(); cin.sync(); cin.ignore(); return 0; }``````

Am I misusing push_back()? I can already see that using push_back() in my inner while loop is going to cause it to not work the way I would have wanted, but is that what is casing a subscript range error?

Thanks all.
In the following snippet:
 ``123456`` `````` while (words[i] == distinct[count]) { distinctCount.push_back(temp); ++temp; ++i; }``````

You increase i but you never insure it doesn't go past the end of your vector.

 ``1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950`` ``````#include #include #include #include int main() { using namespace std ; vector words ; cout << "Enter the words, followed by Ctrl + Z:"; // Unix: Ctrl + D string word ; while( cin >> word ) words.push_back(word) ; // invariant: words contains all the entered words // sort the data sort(words.begin(),words.end()); // create vectors for unique words and a count variable vector distinct ; vector distinctCount ; // test for word distinctness and store values in new vectors string previous_word = "" ; for( vector::size_type i = 0 ; i < words.size() ; ++i ) { if( words[i] != previous_word ) // this is a new distinct word { distinct.push_back( words[i] ) ; // add it to distinct distinctCount.push_back(1) ; // and add a count of 1 previous_word = words[i] ; // and this now becomes the previous word } else // not distinct: this is a repetition of the previous word { distinctCount.back() += 1 ; // just increment the count of the last word } } // output the data for( vector::size_type i = 0 ; i < distinct.size() ; ++i ) { cout << distinct[i] << ": " << distinctCount[i] << '\n' ; } // hold the execution window open cin.clear(); cin.sync(); cin.ignore(); }``````
Topic archived. No new replies allowed.

You are using a version without Ads of this website. Please, consider donating: