IDE has problem handling vectors

Hi, as usual I am pretty new to C++ 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...
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
#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
You are going out of the bounds of your array with i <= words.size(). Use i < words.size() instead.
Wow, that's it!
but why? it does seem logical to me.
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.
So simple...
Kinda amazing. Thanks for quick, precise and indepth help!
1
2
for(vector<int>::iterator i=words.begin(); i!=words.end(); ++i)
        cout << (*i);


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