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?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
// 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.
> 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 )
1
2
3
vector<string> distinct;

vector<int> 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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
// 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?

Thanks all.
In the following snippet:
1
2
3
4
5
6
		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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>

int main()
{
    using namespace 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();
}
Topic archived. No new replies allowed.