vectors out of range

hello,
let's say that I have this vector:

vector hello < int >;

then I go.. hello.push_back(); a couple of times.

How can I write a piece of code that throws an error as soon as it gets out of range and prints something on screen? If I don't write any code and some part of the vector that doesn't actually exist gets mistakenly accessed, I get some sort of warning complaining about the range but I don't know what vector! also.. the program crashes and that's not good.

:(
Use vector.at() function to access vector indexes. Its has exception handling that will throw "out of range" if you try to access inaccessible data.

1
2
3
4
5
6
7
vector<int> vec.
vec.push_back(4);
vec.push_back(3);

cout<<vec.at(0);	//4
cout<<vec.at(1);	//3
cout<<vec.at(2);	//program stops and throws out of range exeption. 
Thanks!

but that's what I am currently using and it still won't say what vector is out of range. For each access I wrote

1
2
3
4
5
6
7
8
9
10
//SUM ALL FREQUENCIES
        if( frequencies.at( i ) )
        {
            sum += frequencies.at( i );
        }
        else
        {
            cout << "ERROR 1";
            return 0;
        }


instead of
1
2
//SUM ALL FREQUENCIES
        sum += frequencies.at( i );


but it still doesn't solve the problem.
The RANGE error keeps showing up without anything useful being printed on screen :(
Last edited on
Topic archived. No new replies allowed.