Testing whether an iterator points to the last item?

前端 未结 9 922
旧巷少年郎
旧巷少年郎 2020-12-23 11:31

I have an stl iterator resulting from a std::find() and wish to test whether it is the last element. One way to write this is as follows:

mine *match = some         


        
9条回答
  •  -上瘾入骨i
    2020-12-23 12:14

    Why do you need to do special behavior only if the item is the last one?

    What about this. The plan is just to compare the address of the iterator's item with the address of the last item in the container, with a check to make sure the item is actually not already the end (making the back call safe):

    if (itr != Mine.end() && &*itr == &Mine.back()) {
      doSomething;
    }
    

提交回复
热议问题