|
-
April 28th, 2004, 07:30 PM
#1
stl list & iterators
I've got a list for XML objects. i'm using the std::list
i declare an iteraror, and set it to the beginning of the list
Code:
XML::iterator i = this->begin();
then i want to go to the 'num' positon of the list, so i use this for bucle:
Code:
for(int ii=0; ii<num ;i++, ii++);
that code is ok, i can see what's on the list.
The problem is that i don't know how to delete that position. the following code doesn't seem to work...
do you have any ideas? this is my first project with stl.. i've read Eckels Thinking In C++ vol 2 and i can't find anything.. he speaks about a remove method, but i can't use it
thanks!!
-
April 28th, 2004, 07:40 PM
#2
Erase only takes one iterator parameter. It's simply .erase(i).
But... What's with the usage of the this pointer here? You aren't by any chance deriving from std::list or something? In that case.. Don't.
And what is XML? Hopefully just a typedefed specialization of std::list?
edit: typos..
Last edited by Assmaster; April 28th, 2004 at 07:43 PM.
-
April 28th, 2004, 08:16 PM
#3
You can use std::advance() algorithm to move an
iterator from its current position. see code ...
Code:
#include <list>
#include <iostream>
#include <algorithm>
#include <iterator>
using namespace std;
int main()
{
list<int> my_list;
for (int i=0; i<10; ++i)
my_list.push_back(i);
list<int>::iterator it = my_list.begin();
int num = 5;
if (num < (int)my_list.size())
advance(it,num);
my_list.erase(it);
copy(my_list.begin(),my_list.end(),ostream_iterator<int>(cout,"\n"));
return 0;
}
Last edited by Philip Nicoletti; April 28th, 2004 at 08:22 PM.
-
April 28th, 2004, 08:33 PM
#4
yes, i am deriving from std::list
Code:
class XML;
typedef std::list<XML*> listaXML;
class XML:private listaXML
{
...
void erase(XML::iterator from, XML::iterator to);
}
i'm working on someone's code.. this is something i have to do for university, so i HAVE to use that code. Erase has been overloaded, it doesn't accept 1 parameter:
Code:
void XML::erase(XML::iterator from, XML::iterator to)
{
listaXML::iterator i = from;
for(;i != to; i++)
delete *i;
this->listaXML::erase(from,to);
}
that's why i called
this->erase(i,i);
-
April 28th, 2004, 09:16 PM
#5
std::list does have a two argument erase, does your
overload erase() work the same way ? Remember,
when you give a range ot iterators, the "end" iterator
points to one PAST the last element you want to erase.
So the folowing doesn't really make sense :
If your overloaded erase works the same as normal
STL functions work (and it should to prevent confusion),
you would need to do something like this :
Code:
list<int>::iterator it2 = my_list.begin();
advance(it2,5);
list<int>::iterator it1 = it2;
++it2;
my_list.erase(it1,it2);
-
April 28th, 2004, 09:51 PM
#6
thanks phil!
that did it!!
problem solved
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|