Vector iterators

后端 未结 7 1041
再見小時候
再見小時候 2021-01-29 04:45

I have a the following code.

vector* irds = myotherobj->getIRDs();//gets a pointer to the vector
for(vector::iterator it = ir         


        
7条回答
  •  无人共我
    2021-01-29 05:33

    You need to use != with iterators to test for the end, not < like you would with pointers. operator< happens to work with vector iterators, but if you switch containers (to one like list) your code will no longer compile, so it's generally good to use !=.

    Also, an iterator is not the type that it points to, so don't try to cast it. You can use the overloaded operator-> on iterators.

    vector* irds = myotherobj->getIRDs();//gets a pointer to the vector
    for(vector::iterator it = irds->begin(); it != irds->end(); ++it)
    {
        it->dosomething();
    }
    

提交回复
热议问题