Click here to Skip to main content
15,921,156 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,
How to erase an n-th element in STL List container.
Can Any one help me.
Posted
Updated 16-Aug-10 3:19am
v2
Comments
Nuri Ismail 16-Aug-10 9:21am    
Changed subject line to something more specific than "About List" and added 'C++' tag. :)

1 solution

You can do this by using erase[^] method of std::list. Here is an example code snippet:
C++
std::list<int> mylist;
std::list<int>::iterator it;

// Fill the list here
for (int i=0; i<10; i++) 
    mylist.push_back (i);

// Init the iterator to point the 1-th element
it = mylist.begin();

// Advance to n-th element (for example we want to erase the 6-th 
// element and "it" points to first elemnt, so we have to advance 5 elements)
std::advance(it, 5);

// Erase the 6-th element
mylist.erase(it);


:)
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900