tags:

views:

2135

answers:

3

Hi there,

As a newbie I'm trying to implement a sorting function in C++, using the list-class. However, running the code I get the error that the list iterator is not incrementable... However it seems very unlikely as it should be incrementable!

code:

void shuffle (list<int> &list1)
{
    list<int> smaller;
    list<int> larger;

    if (list1.size() > 1)
    {
     list<int>::iterator it;
     //int it;

     int x = list1.front();


     for (it = list1.begin(); it != list1.end(); it++)
     {          
      if(*it <= x)
      {
       smaller.push_front(*it);
       list1.pop_front();

      }
      else
      {
       larger.push_back(*it);
       list1.pop_front();
      }
      shuffle (smaller);
      shuffle (larger);
     }
    }
    else
    {
     print(smaller);
     print(larger);

     //cout << "No sorting needed! The list still looks like: ";
     //print(list1);
    }
    print(smaller);  
    print(larger);
}

I implemented this function just in de CPP file, under the main.

Does anyone has any suggestions?

A: 

I was also able to compile the posted code with VS2008 after I commented out the calls to print() and added the following to the beginning:

#include <list>
using namespace std;
Dusty Campbell
A: 

This is my main:

> int _tmain(int argc, _TCHAR* argv[])
{
//DEFINE LIST
list <int> list1;
//FILL LIST
list1.push_front(5);
list1.push_front(2);
list1.push_front(1);
list1.push_front(9);
list1.push_front(12);
list1.push_front(3);
list1.push_front(4);
//PRINT LIST BEFORE SORTING
print(list1);
//SORT LIST

shuffle(list1);



//PRINT AFTER SORTING

system("pause");




return 0;

And the error message is just 1, namely if I debug it (press F5 in VC++ 2008) I get a pop-up that list iterator is not incrementable

+12  A: 

Your call to list1.pop_front() removes the element which the iterator is pointing to initially, invalidating it. And an invalid iterator can not be incremented. :)

It took a few minutes to find with the debugger. Just keep an eye on the value of 'it' as you step through the program. I don't know if you know how to use the debugger yet, but if not, do yourself a favor and learn it. It's an invaluable tool.

(By the way, in the future, please be explicit about whether the error occurs at compile-time or while running the program. Your question stated the error occurred when "compiling the program". I just edited the question for you, hope you don't mind. But it's an important distinction, and makes it harder to answer your question accurately)

jalf
Ok, I'm sorry about telling where it went wrong. Should keep an eye on it.But how to adjust the program, the only thing I w
Well, a simple fix would be to remove "it++" from the for loop, and instead do it just before pop_front(), so the iterator points to the element past the one you're popping. By the way, prefer ++it over it++. :)
jalf
@jalf - good catch. I didn't even look at the code except to get it to compile (adding a dummy print() function). Just lazy I guess.
Michael Burr
But I can not remove an element of the for-loop! There always must be an element in the for loop. Or do I have to change it to a while-loop?
Nah, you can remove it. "for (it = list1.begin(); it != list1.end();)" is perfectly legal. In fact, all three "parts" of a for loop may be empty. for(;;) is the idiomatic way to write an infinite loop, for example ("while(true)" gives a warning on several compilers, for(;;) doesn't)
jalf
Thanks! The error message is at least gone for now...