p() and a list of integers il,il satisfying p().
typedef std::list<int> int_list
typedef int_list::iterator ilst_iter
filter(int_list & il, predicate p)
ilst_iter i
for (i = il.begin(); i != il.end(); i++)
if (p(*i))
il.erase(i)
struct overwrite_iterator {
Container & c;
Container::iterator iter;
operator = (val)
if (iter == c.end())
c.push_back(val)
else
*iter++ = val
}
.insert() and .erase() invalidate all iterators to the
right of the involved iterator.
.resize() and .reserve(), which may invalidate
all interators (or may not).
.end().
.begin().
filter(int_list & il, predicate p)
ilst_iter i
for (i = il.begin(); i != il.end(); i++)
if (p(*i))
il.erase(i)
*i invalidates i; neither i != nor i++
have defined behavior.
ilst_iter i for (i = il.begin();i != il.end();i++) if (p(*i)) il.erase(i)
ilst_iter i = intl.begin()
while i != intl.end()
ilst_iter this = i++
if p(*this)
intl.erase(this)
struct overwrite_iterator {
Container & c;
Container::iterator iter;
operator = (val)
if (iter == c.end())
c.push_back(val)
else
*iter++ = val
}
.push_back() invalidates the .end() iterator stored in
iter.
if (iter == c.end()) c.push_back(val) iter = c.end() else *iter++ = val
This page last modified on 17 November 2003.