What happens if you increment an iterator that is equal to the end iterator of an STL container

前端 未结 8 1491
别那么骄傲
别那么骄傲 2020-11-27 05:46

What if I increment an iterator by 2 when it points onto the last element of a vector? In this question asking how to adjust the iterator to an STL container by 2 elements t

8条回答
  •  温柔的废话
    2020-11-27 06:37

    Even though this question is half a year old, it might still be useful to mention the use of comparison operators > and < to check if you iterated past the end (or the start when iterating back) of the container. For example:

    vector vec;
    vec.push_back( 1 );
    vec.push_back( 2 );
    
    vector::iterator it = vec.begin();
    
    it+=10; //equivalent to advance( it, 10 )
    bool isPastEnd = it > vec.end(); //true
    

提交回复
热议问题