Topic: STL list iterators


Author: Harald Iwe <hiw@funcom.com>
Date: 1997/09/11
Raw View
I have a STL list which holds some Item_t objects. In addition I have
an iterator which points to one of the objects in the list.

If I do a push_front(Item_t) call on this list is the iterator still
valid ?

Is my iterator still valid if I do a erase(a_second_iterator) call ?

In general which operations can be done on the different STL
containers without invalidating existing iterators ?

class Item_t {
}

int main() {
  list<Item_t> ItemList;
  ItemList.push_front(Item_t());
  ItemList.push_front(Item_t());
  ItemList.push_front(Item_t());

  list<Item_t>::iterator iItemList1 = ItemList.begin();

  ItemList.push_front(Item_t());

  // is iItemList1 still valid ?

  list<Item_t>::iterator iItemList2 = ItemList.end();
  ItemList.erase(iItemList2);

  // is iItemList1 still valid ?

  return 0;
}

--
Harald Iwe, hiw@funcom.com
Funcom Oslo A/S, Karenlyst alle' 5, N-0277 Oslo, Norway
---
[ comp.std.c++ is moderated.  To submit articles: Try just posting with your
                newsreader.  If that fails, use mailto:std-c++@ncar.ucar.edu
  comp.std.c++ FAQ: http://reality.sgi.com/austern/std-c++/faq.html
  Moderation policy: http://reality.sgi.com/austern/std-c++/policy.html
  Comments? mailto:std-c++-request@ncar.ucar.edu
]





Author: Volker Hetzer <hetzer.abg@sni.de>
Date: 1997/09/12
Raw View
Harald Iwe wrote:
>
> I have a STL list which holds some Item_t objects. In addition I have
> an iterator which points to one of the objects in the list.
>
> If I do a push_front(Item_t) call on this list is the iterator still
> valid ?
>
> Is my iterator still valid if I do a erase(a_second_iterator) call ?
>
> In general which operations can be done on the different STL
> containers without invalidating existing iterators ?
The STL doc tells you for each container when the iterators become
invalid.
I believe, it amounts to the following:
Lists,sets,multisets,maps,multimaps: When you delete the object the
iterator points to.
Vectors, queues: Whenever the size of the container changes.

Volker
---
[ comp.std.c++ is moderated.  To submit articles: try just posting with      ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu         ]
[ FAQ:      http://reality.sgi.com/employees/austern_mti/std-c++/faq.html    ]
[ Policy:   http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu                             ]