Vector iterators

后端 未结 7 1031
再見小時候
再見小時候 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:35

    Why do you want to get a pointer?

    Use a reference:

    for(vector::iterator it = irds->begin(); it != irds->end(); ++it)
    {
        IRD & ird = *it;
        ird.doSomething();
    }
    

    Alternatively:

    for(vector::iterator it = irds->begin(); it != irds->end(); ++it)
    {
        it->doSomething();
    }
    

    Also, as everyone said, use != when comparing iterators, not <. While it'll work in this case, it'll stop working if you use a different container (and that's what iterators are for: abstracting the underlying container).

提交回复
热议问题