IDE has problem handling vectors

Jun 3, 2010 at 11:38pm
Hi, as usual I am pretty new to Python and just started fighting with vectors.
And I stumbled upon a problem I cannot figure out.
Whenever I try to do some operation with vector.size() the IDE (visual express 2010/08, dev++ ) throw in a towel:
An unhandled exception of type 'System.Runtime.InteropServices.SEHException' occurred in cenzorship vectors.exe

Additional information: External component has thrown an exception.

Any ideas how to solve this one?
PS: I did some searching for solution but coudn't find, feel free to abuse me for it...
Jun 3, 2010 at 11:43pm
Welcome to the forum where people tell you not to reduce yourself (encouraging abuse, saying that "as usual" you are new). Really.

Could we see your code? This exception is quite a bugger, I hear (I never got it myself in any of my programs).

-Albatross
Last edited on Jun 3, 2010 at 11:43pm
Jun 3, 2010 at 11:48pm
#include "StdAfx.h"
#include <iostream>
#include <vector>
#include <string>
using namespace std;


int main()
{
vector<int> words(0);
int k;
while ( cin >> k )
{
words.push_back(k);
for ( int i = 0; i<=words.size(); ++i )
cout << words[i];

}

cin.get();
}




There you are. It is like the simplest version that bears the problem I could find
As for reducing part. I am kinda desperate now.
Last edited on Jun 3, 2010 at 11:49pm
Jun 3, 2010 at 11:52pm
You are going out of the bounds of your array with i <= words.size(). Use i < words.size() instead.
Jun 4, 2010 at 12:01am
Wow, that's it!
but why? it does seem logical to me.
Jun 4, 2010 at 12:06am
Because when there are say, five elements in the vector, they have the indexes 0, 1, 2, 3 and 4.
size() returns 5 and if you use <= the loop body will be also executed when i reaches 5. But words[5] doesn't exist.
Jun 4, 2010 at 12:11am
So simple...
Kinda amazing. Thanks for quick, precise and indepth help!
Jun 4, 2010 at 5:38pm
1
2
for(vector<int>::iterator i=words.begin(); i!=words.end(); ++i)
        cout << (*i);


Never woulda happened !!
Topic archived. No new replies allowed.