www.delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/2001/07/17/13:00:17

From: Niklas_Pson AT nosmam DOT hotmail DOT com (Niklas Pettersson)
Newsgroups: comp.os.msdos.djgpp
Subject: removing objects from a container... (iterators)
Date: 17 Jul 2001 16:56:34 GMT
Organization: Lund Institute of Technology, Sweden
Lines: 38
Message-ID: <90E1C8809NiklasPsonnospamhotm@130.235.20.4>
NNTP-Posting-Host: npedt97.univ.vxu.se
User-Agent: Xnews/03.04.11
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp
Reply-To: djgpp AT delorie DOT com

Hello!

I have a program that has two nested iterators.. The inner iterator loop 
removes objects from the container.. This makes the outer iterator 
invalid.. Is there any nice way to solve this.. The "same" problem accures 
if only one iterator is used (altough that is easily solved)..
Includes some code for the simple case

#include <iostream>
#include <list>


int main()
{
	typedef std::list<int *> TestListType;
	
   TestListType l;
	l.push_back(new int(10));
	l.push_back(new int(20));
	l.push_back(new int(30));
	
	TestListType::iterator j;
	
	for (j = l.begin(); j != l.end(); j++)
	{
		int *int_obj = *j;
		
		std::cout << *int_obj << std::endl;
		
		if (*int_obj == 20)
			l.remove(int_obj); // THIS MAKES ITERATOR INVALID??
	}
	
	return (0);
}


/ Niklas

- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019