开发者

C++ iterator of list [duplicate]

This question already has an answer here: 开发者_如何学JAVA Closed 11 years ago.

Possible Duplicate:

C++: Check if iterator belongs to a list

How can I check if an iterator recieved by function belongs to the right list?

List<double> doublesList;
List<double> doublesList2;

// Can't mix iterators
ASSERT_THROW(ListExceptions::ElementNotFound, doublesList.remove(doublesList2.begin()));
ASSERT_THROW(ListExceptions::ElementNotFound, doublesList2.insert(5.0, doublesList.begin()));

I askes for the case of my own Linked List and my own Iterator. Thanks a lot.


With standard containers, you can't. The C++ standard library leaves this up to the programmer. Comparison of iterators invokes UB, in fact.

With homegrown containers, you can insert information into the iterators so that they remember which container they belong to; that makes the iterators heavier, of course, so it's often only sensible in debug mode.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜