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?
// 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<string> 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<string> distinct;
int count = 0;
vector<int> distinctCount;
// test for word distinctness and store values in new vectors
typedef vector<string>::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.
// 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<string> 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<string> distinct;
int count = 0;
vector<int> distinctCount;
// test for word distinctness and store values in new vectors
typedef vector<string>::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?
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
int main()
{
usingnamespace std ;
vector<string> 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<string> distinct ;
vector<int> distinctCount ;
// test for word distinctness and store values in new vectors
string previous_word = "" ;
for( vector<string>::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<string>::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();
}