Topic: erase of last element in deque.


Author: CornedBee <wasti.redl@gmx.net>
Date: Wed, 27 Jan 2010 22:03:44 CST
Raw View
On Jan 25, 6:41 pm, rogero <roger....@gmail.com> wrote:
> I have a question about the wording of 23.3.2.3p4 which states:
>
> "An erase at the beginning of the deque invalidates only the
> iterators and the references to the erased elements. An erase at the
> end of the deque invalidates only
> the iterators and the references to the erased elements and the past-
> the-end iterator."
>
> So should this program invalidate 'end1' or not?
>
> #include <deque>
> #include <iostream>
>
> int main()
> {
>     std::deque<int> coll;
>
>     coll.push_back(0);
>
>     std::deque<int>::iterator end1 = coll.end();
>     coll.erase(coll.begin());
>     std::deque<int>::iterator end2 = coll.end();

IMO, yes. "At the end" doesn't simply refer to the names of the
functions you used to obtain the iterators, but the actual contents of
the container. If coll.begin() == coll.end()-1, as is the case in your
example, then coll.erase(coll.begin()) is an erase at the end of the
container.

Sebastian


--
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@netlab.cs.rpi.edu]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html                      ]





Author: rogero <roger.orr@gmail.com>
Date: Mon, 25 Jan 2010 11:41:57 CST
Raw View
I have a question about the wording of 23.3.2.3p4 which states:

"An erase at the beginning of the deque invalidates only the
iterators and the references to the erased elements. An erase at the
end of the deque invalidates only
the iterators and the references to the erased elements and the past-
the-end iterator."

So should this program invalidate 'end1' or not?

#include <deque>
#include <iostream>

int main()
{
    std::deque<int> coll;

    coll.push_back(0);

    std::deque<int>::iterator end1 = coll.end();
    coll.erase(coll.begin());
    std::deque<int>::iterator end2 = coll.end();

    std::cout << coll.size() << " items" << std::endl;
    std::cout << "end1 == end2? " << (end1 == end2) << std::endl;

    return 0;
}

Regards,
Roger.

--
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@netlab.cs.rpi.edu]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html                      ]