954,504 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Another Vector Subscript Overload

So I always seem to encounter this error. I usually find some odd hackish fix for it but this time I'm having too much trouble. I'm basically trying to make a program which corrects words the user types in (similar to that of any modern phone). The function I'm posting below is an attempt to make a vector of vectors. I am associating every letter with it's neighbor keys. For example...

If the user types in the letter 'A', I want a vector with the value 'A' to hold every letter that the 'A' key touches (Q, W, S, and Z). Every time I compile I get a subscript error. Any help would be appreciated.

vector< vector<string> > letterNeighbors; // VECTOR OF VECTORS

ifstream fin("iPhone_keyboard.txt"); // IPHONE KEYBOARD LIST
while ( fin ) // WHILE THE CONSOLE CAN TAKE INPUT
{
				
	char templateLetter; // FIRST LETTER
	string neighborLetter; // NEIGHBORING LETTERS

	fin >> templateLetter; // GETS THE FIRST CHARACTER OF THE LINE (TEMPLATE)
	getline(fin, neighborLetter); // GETS THE STRING OF CHARACTERS NEXT TO IT (NEIGHBORS)

	int neighborSize = neighborLetter.size(); // USED TO REFERENCE NUMBER OF ITERATIONS FOR LOOP
	int i = 0;

	while ( i < neighborSize )
	{
	letterNeighbors.resize( 123, vector<string> (neighborSize) ); // RESIZES VECTOR
	letterNeighbors[templateLetter][i] = neighborLetter.substr(i);	// MAKES A VECTOR WITH THE TEMPLATE LETTER AND GRADUALLY ADDS NEIGHBORS
	i++;
	}

}
khsheehan
Newbie Poster
5 posts since May 2011
Reputation Points: 10
Solved Threads: 0
 

What's the error? Compile time or run time? If run time, have you tried putting in code to determine just which statement is causing the trouble?

arkoenig
Master Poster
703 posts since Jun 2010
Reputation Points: 359
Solved Threads: 109
 

Sorry, the error is in run time. I have tried resizing the vector multiple times. I get an error when the program tries to assign values...

letterNeighbors[templateLetter][i] = neighborLetter.substr(i);	// MAKES A VECTOR WITH THE TEMPLATE LETTER AND GRADUALLY ADDS NEIGHBORS


I only assign the number of values because of the error. The run time error persists even when the line where I resize the vector is removed.

khsheehan
Newbie Poster
5 posts since May 2011
Reputation Points: 10
Solved Threads: 0
 

Ok, so I have been messing around with the size of the vector and I decided to change the resize...

letterNeighbors.resize( 123, vector (neighborSize) );

to

letterNeighbors.resize( 600, vector (600) );

It works when I make the vector that large but it takes a longer time to run through the program. Any idea why it needs to be such a high number / how I can make it lower?

khsheehan
Newbie Poster
5 posts since May 2011
Reputation Points: 10
Solved Threads: 0
 

There is likely a default vector size, and 123 is smaller than the default, hence the error. Just a guess.

rubberman
Posting Virtuoso
1,564 posts since Mar 2010
Reputation Points: 277
Solved Threads: 179
 

The default vector size is 0.

arkoenig
Master Poster
703 posts since Jun 2010
Reputation Points: 359
Solved Threads: 109
 

I knew that the default vector size was 0, I just thought that one of the reasons to use vectors was that they could resize themselves based on input. Even if a vector starts with a size of 0, when I add values it should automatically change it's size. Is that not correct? I would assume I could only get a "Subscript out of range" error if I specified the vector size and then tried to assign values outside of that range.

khsheehan
Newbie Poster
5 posts since May 2011
Reputation Points: 10
Solved Threads: 0
 

If v is a vector, v[i] has undefined behavior if i is out of range (i.e. if i >= v.size()). Accessing v[i] does not create that element (or any element) if that element does not already exist.

If you want to append a new element x to v, you can execute v.push_back(x), which will increase the size of v by one, and put a copy of x into the newly created element.

arkoenig
Master Poster
703 posts since Jun 2010
Reputation Points: 359
Solved Threads: 109
 

I guess I could solve the problem by including a push_back statement in the loop which would automatically add one value to the vector and then have it replaced immediately afterwards. Either way I am sticking with a resize I have found that works. It isn't the most efficient way but it works. Thanks.

khsheehan
Newbie Poster
5 posts since May 2011
Reputation Points: 10
Solved Threads: 0
 

I do not understand what you expect these two statements to accomplish:

letterNeighbors.resize( 123, vector<string> (neighborSize) );
letterNeighbors[templateLetter][i] = neighborLetter.substr(i);

After you have executed the first of these statements, letterNeighbors will have exactly 123 elements, each of which will be a vector. So when you execute the second statement, if the value of templateLetter is >= 123, the result will be a subscript out of range.

arkoenig
Master Poster
703 posts since Jun 2010
Reputation Points: 359
Solved Threads: 109
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: