We help IT Professionals succeed at work.

STL deque

adopo
adopo asked
on
I'm thinking about trying to use a deque container but I'm not sure if it is appropriate for my application.

I need a FIFO queue for strings so that I can add messages as they are received and pull them as I'm ready to process them.  So I thought I'd add them to the end of the deque and pull them from the beginning of the deque.  

Do I need to specify the size of the deque up front?  What happens if I later need more room, would I have to redo the entire deque or is there some way to expand it "in-place".  

If deque is not a good choice, what would be?  Any examples, suggestions, ideas are appreciated.


Comment
Watch Question

AxterSenior Software Engineer

Commented:
>>Do I need to specify the size of the deque up front?  
No you don't.  It will expand automatically as required.


>>What happens if I later need more room, would
>>I have to redo the entire deque or is there some way to
>>expand it "in-place".  

You don't need to redo the deque.  The order will stay intact.

Author

Commented:
Thanks very much for your response! I was hoping that was the answer - it sounds like I'm on the right track.  Do you happen to know of an example that uses a deque as a FIFO?

Thanks again
Senior Software Engineer
Commented:
Simple example:

#include <deque>
#include <string>
#include <iostream>

using namespace std;

int main(int argc, char* argv[])
{
     deque<string> TicketsList;
     TicketsList.push_back("Tom");
     TicketsList.push_back("Sue");
     TicketsList.push_back("David");
     TicketsList.push_back("Marry");

     cout << TicketsList.begin()->c_str() << endl;
     TicketsList.pop_front();

     cout << TicketsList.begin()->c_str() << endl;
     TicketsList.pop_front();

     TicketsList.push_back("Joe");
     TicketsList.push_back("Sally");

     cout << TicketsList.begin()->c_str() << endl;
     TicketsList.pop_front();

     cout << TicketsList.begin()->c_str() << endl;
     TicketsList.pop_front();

     cout << TicketsList.begin()->c_str() << endl;
     TicketsList.pop_front();

     cout << TicketsList.begin()->c_str() << endl;
     TicketsList.pop_front();


     return 0;
}

Commented:
If you only want FIFO functionality, then the queue class may be better than deque.  You only work with push() and pop() functions, simplifying the code a bit.

Explore More ContentExplore courses, solutions, and other research materials related to this topic.