CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 10 of 10

Thread: Lists STL

  1. #1
    Join Date
    Jan 2008
    Posts
    60

    Lists STL

    small question about LIST stl...
    My code looks something like this...
    Code:
    #include <list>
    #include <iostream>
    using namespace std;
    
    
    void main()
    {
    	list<int> randomNums;
    	int number20 = 0;
    	while( number20 < 5 )
    	{
    		(number20++);
    		randomNums.push_back((2*number20) + 1);
    		randomNums.push_back(2*number20);
    	}
    	list<int>::iterator iter = randomNums.begin();
    
    	randomNums.erase(randomNums.end());
    
    	for( list<int>::iterator iter = randomNums.begin() ; iter != randomNums.end() ; iter++)
    	{
    		cout << (*iter) << endl;
    	}
    
    }
    And i get en error when trying to use the erase command with the randomNums.end(); which makes sense.
    >>>HOW CAN I ERASE THE LAST ELEMENT FROM THE LIST?

    also when I try to erase using this code:
    [code]
    randomNums.erase(iter++);
    [/coode]
    it won't erase the second entry.
    >>WHY DOES LIST FUNCTION LIKE THIS??

  2. #2
    Join Date
    Jul 2002
    Location
    Portsmouth. United Kingdom
    Posts
    2,727

    Re: Lists STL

    randomNums.end() returns the iterator to one past the last element in the list.
    If you want to erase the last element then you you need to subtract 1 from the end() iterator or use rbegin() to get the iterator.

    Also
    randomNums.erase(iter++) increments the iterator after setting the parameter for the function.
    Last edited by JohnW@Wessex; April 1st, 2008 at 05:20 AM.

  3. #3
    Join Date
    Jan 2008
    Posts
    60

    Re: Lists STL

    I see --- so "--iter" would fix the problem!!!
    also I was reading this book and I noticed this example which I've never seen it before... what does it signify?
    Code:
    list<char> list1 = make< list<char> >("remembering");
    whats the make for? In my last example I didn;t use it.

  4. #4
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    Re: Lists STL

    If you want to erase the last element then you you need to subtract 1 from the end() iterator or use rbegin() to get the iterator.
    list also has pop_back(), so it makes sense to use it instead of erase() for the last element.

    also I was reading this book and I noticed this example which I've never seen it before... what does it signify?
    I do not think make() is in the C++ standard library, so what it does depends on the implementation.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  5. #5
    Join Date
    Jan 2008
    Posts
    60

    Re: Lists STL

    Well if your specifically want to delete the last element yes, but if it's a general loop that deletes lets say blocks every time a ball hits them, then u need to use erase.
    Thx all.

  6. #6
    Join Date
    Jan 2008
    Location
    California, USA
    Posts
    822

    Re: Lists STL

    Quote Originally Posted by armen_shlang
    Well if your specifically want to delete the last element yes, but if it's a general loop that deletes lets say blocks every time a ball hits them, then u need to use erase.
    Thx all.
    Hello there,

    you didn't quite understand what laserlight meant.
    in the example you gave us,
    1) your approach to delete the element in the list is encumbered in comparison with what laserlight suggested.
    2) you have to be very careful when you use an iterator to delete an element - especially in a loop you're thinking about - thus, your approach is more error-prone.

    hope this helps.

  7. #7
    Join Date
    May 2007
    Location
    Bangalore India
    Posts
    262

    Re: Lists STL

    I agree with potatocode deleting in a loop is error prone as it might invalidate your iterator. Got to be careful.
    Dont forget to rate my post if you find it useful.

  8. #8
    Join Date
    Jul 2002
    Location
    Portsmouth. United Kingdom
    Posts
    2,727

    Re: Lists STL

    According to 'Effective STL', for associative containers (set, map) you should use

    container.erase(itr++);

    For sequence containers (vector, string deque) you should use

    itr = container.erase(itr);

    For 'list' you can use either.

  9. #9
    Join Date
    Feb 2003
    Posts
    377

    Re: Lists STL

    Yes, but deleting in a loop is more complicated because you usually have a condition and you want the iterator to increment when that condition is not met. People often use for loops which include an increment of the iterator for each pass through the loop, but that causes problems if you use either of the above choices.

  10. #10
    Join Date
    Jul 2002
    Location
    Portsmouth. United Kingdom
    Posts
    2,727

    Re: Lists STL

    Quote Originally Posted by jlou
    , but that causes problems if you use either of the above choices.
    Yes, I didn't show the full extract from the book, just the erase lines. The lines showed how to have a valid iterator after erasing.

    The expanded code is...

    Associative containers
    Code:
    itr = container.begin();
    
    while (itr != container.end()
    {
    	if (condition)
    	{
    		container.erase(itr++);
    	}
    	else
    	{
    		++itr;
    	}
    }
    


    Sequence containers
    Code:
    itr = container.begin();
    
    while (itr != container.end()
    {
    	if (condition)
    	{
    		itr = container.erase(itr);
    	}
    	else
    	{
    		++itr;
    	}
    }
    

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured