Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <list>
- #include <cassert>
- template<class reverse_iterator>
- typename reverse_iterator::iterator_type reverse_to_normal(reverse_iterator i)
- {
- ++i; // look at reverse_iterator documentation why this is necessary
- return i.base();
- }
- int main()
- {
- std::list<int> list; /* same behavior with std::set */
- /* Let's fill our container a bit */
- list.push_back(1);
- list.push_back(2);
- std::list<int>::reverse_iterator back1 = list.rbegin();
- std::list<int>::iterator back2 = reverse_to_normal(back1);
- assert(*back1 == 2);
- assert(*back2 == *back1); /* so far, so good */
- /* Let's fill our container a bit more. set and list guarantee that iterators
- * stay valid so there is no harm in continuing to use back1 and back2, right?
- */
- list.push_back(3);
- assert(*back2 == *back1); /* fails. back1 now points to 3, back2 is still ok */
- /* Lesson learned: Iterator validity only applies to forward iterators */
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment