CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    Nov 2005
    Posts
    106

    Emptying a Queue

    Hello everyone,

    The following function outputs the information in two double edged queues and using this output updates the CList control to display the required colour. It then pushes the elements in the deques one by one into two new deques and removes them from the orignal deques.

    Code:
    void queue_print(CPrototype3_0Dlg *mDlg)
    {  
    	
    	while (!rowq.empty() && !colq.empty())
    	{	
    		mDlg->m_list.SetSubItemColor(rowq.front(), colq.front(), RGB(0,0,255),RGB(128,0,0) );
    		rowbuff.push_front(rowq.front());
    		colbuff.push_front(colq.front());
    		rowq.pop_front();
    		colq.pop_front();
    	}
    }
    I've written the following function to empty the four queues that Im using in my application. However they do not seem to work as intended, in fact it does not seem to do anything at all.

    Code:
    void empty_queue()
    {
    while (!rowq.empty()&&!colq.empty()&&!rowbuff.empty()&&!colbuff.empty())
    	{	
    		rowq.pop_front();
    		colq.pop_front();
    		rowbuff.pop_front();
    		colbuff.pop_front();
    	}
    }
    To my understand this should remove all the elements in all the queues. I've attached both functions to a button. On the click of one button the void print queue function is accessed and the CList control is updated with satisfactory results.

    Then I click the second button and the queues should all be emptied. So that when try to access the queue again it shouldn't be storing any elements. But this is not the case.

    Can anyone tell me if there is any fault in the code above?

    Thank You
    Jaz

  2. #2
    Join Date
    May 2004
    Location
    London, England
    Posts
    563

    Re: Emptying a Queue

    Why don't you just use clear() or the erase(iterator first, iterator last) functions?
    I don't mind that you think slowly but I do mind that you are publishing faster than you think. Wolfgang Pauli, physicist, Nobel laureate (1900-1958)

  3. #3
    Join Date
    Nov 2005
    Posts
    106

    Re: Emptying a Queue

    I didn't know that those functions existed!

    So basically I could use

    Code:
    rowq.clear();
    colq.clear();
    rowbuff.clear();
    colbuff.ckear();
    instead of;

    Code:
    while (!rowq.empty()&&!colq.empty()&&!rowbuff.empty()&&!colbuff.empty())
    	{	
    		rowq.pop_front();
    		colq.pop_front();
    		rowbuff.pop_front();
    		colbuff.pop_front();
    	}
    Also, I managed to figure out why my application wasn't doing what it was supposed to be doing and it nothing to do with emptying the stack.

    Thank You
    Jaz

  4. #4
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: Emptying a Queue

    Also, clear each queue independently of the other. The code you posted is buggy whereby as soon as one queue is empty, the while loop will exit potentially leaving other queues uncleared.

  5. #5
    Join Date
    Nov 2005
    Posts
    106

    Re: Emptying a Queue

    Quote Originally Posted by Arjay
    Also, clear each queue independently of the other. The code you posted is buggy whereby as soon as one queue is empty, the while loop will exit potentially leaving other queues uncleared.
    I thought the statement;

    Code:
    (!rowq.empty()&&!colq.empty()&&!rowbuff.empty()&&!colbuff.empty())
    made sure that the loop would not exit until all the queues were empty. The way I read it is;

    "WHILE rowq is not empty AND colq is not empty AND rowbuff is not empty AND colbuff is not empty DO..."

    So if one queue has an element in it the statement above will return TRUE and the while loop will not exit. Or am I reading this code wrong?

    Thank You
    Jaz

  6. #6
    Join Date
    Feb 2002
    Posts
    5,757

    Re: Emptying a Queue

    true and false not equal true

    Kuphryn

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