lists and erasing

Hello I am trying to erase parts of my list. I create the itterator and i am able to erase the iterator = my_list.begin(); but when i move through the list and i want to delete something at the end or middle it wont let me, for example
1
2
iterator= my_list.end();
my_list.erase(iterator);
wont work :(
Last edited on
This is strange. Take a look at http://www.cplusplus.com/reference/stl/list/erase/ for an example program and see if your code is doing something different when erasing element from a list.
AH I think you have to have two different itertors then, like you cant be scrolling through the list with one iterator and then want to delete that iterator. ill try it now and see if it works thanks
closed account (DSLq5Di1)
The end iterator is not the last element, but an iterator past the last element.

If you simply need to erase elements from the front or back,
http://www.cplusplus.com/reference/stl/list/pop_back/
http://www.cplusplus.com/reference/stl/list/pop_front/

If you have an index and wish to erase an element in the middle,
http://www.cplusplus.com/reference/std/iterator/advance/

If you are iterating through your list to erase elements,
1
2
3
4
5
6
7
8
9
10
11
12
for (auto it = my_list.begin(); it != my_list.end();)
{
    if (...) // erase condition
    {
        it = my_list.erase(it); // erase will invalidate the iterator,
                                // but returns a new iterator to the next element. 
    }
    else
    {
        ++it;
    }
}

Though in that case, a much better solution is to use the remove_if algorithm,
http://en.wikipedia.org/wiki/Erase-remove_idiom

Finally, are you sure list is the appropriate choice for your container?
http://www.linuxsoftware.co.nz/containerchoice.png
Yeah i am working on the Graduation problem http://www.cplusplus.com/forum/articles/12974/

And most people including problem description say to use lists, What is the auto thing you use? I havent seen that before. wouldnt mean an automatic iterator would it?
Ok it is slowly starting to work... although there are still some problems, It seems i cannot delete the last object in the list,

here aare some examples im using a list with ints 1 - 10:

1
2
3
4
5
6
7
	for(iterator=list_of_numbers.begin(); iterator != list_of_numbers.end(); ++iterator)
	{
		if((*iterator)%4 == 0) //delete multiples of 4, this works
		{
			iterator = list_of_numbers.erase(iterator) ;
		}
	}


1
2
3
4
5
6
7
	for(iterator=list_of_numbers.begin(); iterator != list_of_numbers.end(); ++iterator)
	{
		if((*iterator)%2 == 0) //delete multiples of 2, this DOESNT work, im assuming because the last digit 10 screws  it all up.
		{
			iterator = list_of_numbers.erase(iterator) ;
		}
	}


although i read the definition of erase here http://www.cplusplus.com/reference/stl/list/erase/

and it sais it should account for the user erasing the last element by returning this here http://www.cplusplus.com/reference/stl/list/end/
Last edited on
closed account (DSLq5Di1)
What is the auto thing you use?
Type inference -> http://www2.research.att.com/~bs/C++0xFAQ.html#auto

Only increment the iterator if it does not meet the erase condition, see the code in my previous post. Though I recommend using the erase-remove idiom,
#include <algorithm>

1
2
3
4
5
6
auto new_end = remove_if(list_of_numbers.begin(), list_of_numbers.end(), [](const int& n)
{
    return n % 2 == 0; // delete multiples of 2
});

list_of_numbers.erase(new_end, list_of_numbers.end());

I'm using a lambda function here but you could also use a normal function as your predicate.
http://www.cplusplus.com/reference/algorithm/remove_if/
http://en.wikipedia.org/wiki/Anonymous_function#C.2B.2B
okay, so would a break statement be ok? though i remember reading somewhere that anything that uses a break statement can be re-written to not use it.

Another question i have.

1
2
3
4
5
6
7
	for(iterator=list_of_numbers.begin(); iterator != list_of_numbers.end(); ++iterator)
	{
		if((*iterator)%1 == 0) //delete multiples of 1
		{
			iterator = list_of_numbers.erase(iterator) ;
		}
	}


Somehow, this code does not delete the entire list? even though everything should be a multiple of 1 Thanks for your help btw so far

EDIT: The 2 was a typo sorry
Last edited on
closed account (DSLq5Di1)
I'm going to assume the % 2 is a typo, you're still incrementing the iterator after an erase operation.. erase returns the next element after the one you just deleted, then your for loop increments that iterator before reaching your if condition.

1
2
3
4
5
6
7
8
9
10
11
	for(iterator=list_of_numbers.begin(); iterator != list_of_numbers.end(); ++iterator)
	{
		if((*iterator)%1 == 0) //delete multiples of 1
		{
			iterator = list_of_numbers.erase(iterator) ;
		}
		else
		{
			++iterator;
		}
	}
Hmm okay i did what you did and it worked. Ill have to study it to understand exactly why.

First of all though i didnt even know you can have a for loop with only 2 things, i thought you needed a start statement, end statement, and the process it uses to get there... Thanks alot!
closed account (DSLq5Di1)
Try writing that code without a loop, it may help you understand what is happening at each step.
Topic archived. No new replies allowed.