Trouble popping stack using iterator

This is a discussion on Trouble popping stack using iterator within the C++ Programming forums, part of the General Programming Boards category; The following goes into an infinite loop... Code: #include <iostream> #include <stack> #include <list> struct Foo{}; int main() { std::list<std::stack<Foo>> ...

  1. #1
    ...and never returned. StainedBlue's Avatar
    Join Date
    Aug 2009
    Posts
    168

    Trouble popping stack using iterator

    The following goes into an infinite loop...

    Code:
    
    #include <iostream>
    #include <stack>
    #include <list>
    
    struct Foo{};
    
    int main()
    {
    	std::list<std::stack<Foo>> foo_list;
    	std::stack<Foo> first_foos;
    
    	first_foos.push(Foo());
    	first_foos.push(Foo());
    	first_foos.push(Foo());
    
    	foo_list.push_back(first_foos);
    
    	/*  Add some more stacks of foos...
    	*/
    	for(int i = 0; i < 10; i++)
    	{
    		std::stack<Foo> foo_stack;
    
    		for(int j = 0; j < 10; j++)
    		{
    			foo_stack.push(Foo());
    		}
    
    		foo_list.push_back(foo_stack);
    	}
    
    	while(!first_foos.empty())
    	{
                    std::cout << "Stack size: " << first_foos.size() << "\n";
    		std::cout << "Popping...\n";
    
    		std::list<std::stack<Foo>>::iterator first_foos_iter = foo_list.begin();
    		first_foos_iter->pop();
    	}
    
    	return 0;
    }
    With what I'm trying to accomplish, I need to be able to pop the first stack in the list like I'm trying to do above. Problem is, it seems to me, a const iterator is being used, so no popping is actually taking place (during the loop, the stack size remains 3).

    I'm heavily invested in the code thus far, and I really need to iterate over the list, and to use the iterator to pop the first stack when needed.

    Is there a way to do this? Am I just overlooking something obvious?
    goto( comeFrom() );

  2. #2
    ...and never returned. StainedBlue's Avatar
    Join Date
    Aug 2009
    Posts
    168
    Oddly enough, the assertion error I get says: deque empty before pop


    ???
    goto( comeFrom() );

  3. #3
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Oddly enough, you're on the C forum, not the C++ forum.

    Quzah.
    Hope is the first step on the road to disappointment.

  4. #4
    ...and never returned. StainedBlue's Avatar
    Join Date
    Aug 2009
    Posts
    168
    Request Move to C++ Please.
    goto( comeFrom() );

  5. #5
    ...and never returned. StainedBlue's Avatar
    Join Date
    Aug 2009
    Posts
    168
    doesn't matter, I figured it out.

    delete thread if you please moderator.
    goto( comeFrom() );

  6. #6
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,892
    We don't delete threads just because they're solved. However, you should post what you've figured out, though, so that people who find this thread can learn from it.

    I'll do it for you this time. You were removing elements from the first stack in the list, but were checking whether the entire list was empty. Since you weren't modifying the list itself, that condition could never come true.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. stack and pointer problem
    By ramaadhitia in forum C Programming
    Replies: 2
    Last Post: 09-11-2006, 11:41 PM
  2. Question about a stack using array of pointers
    By Ricochet in forum C++ Programming
    Replies: 6
    Last Post: 11-17-2003, 09:12 PM
  3. error trying to compile stack program
    By KristTlove in forum C++ Programming
    Replies: 2
    Last Post: 11-03-2003, 05:27 PM
  4. What am I doing wrong, stack?
    By TeenyTig in forum C Programming
    Replies: 2
    Last Post: 05-27-2002, 02:12 PM
  5. Stack Program Here
    By Troll_King in forum C Programming
    Replies: 7
    Last Post: 10-15-2001, 05:36 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21