question concerning vectors in for loop.

Hello I have a while loop it looks like this.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
for(int i=0;i<list.size();i++)
    {

        if(list[i]=="question")
        {
            cout<<"derp"<<endl;
        }
        else if (list[i]=="dolan")
        {
            cout<<"herp"<<endl;
        }
        else
        {
            cout<<"merp"<<endl;
        }
    }

this is just a test for loop :P

anyways that code works fine..
but if I put in the condition i<=list.size() instead of i<list.size()
the program crashes..

Does anyone have an explanation for this?
can you not test to see if something is equal to the size of a vector?
A list of size X has X items, located at list[0] to list[X-1].

The problem isn't about checking the size; it's about accessing an object that doesn't exist, namely list[X]. You put "i < list.size()" because when "i == list.size()", you've passed the list. Thus, it stops when it reaches list.size().
Topic archived. No new replies allowed.