Page 1 of 1

STL queues A simple "how to" Rate Topic: -----

#1 gabehabe  Icon User is offline

  • GabehabeSwamp
  • member icon




Reputation: 1382
  • View blog
  • Posts: 10,962
  • Joined: 06-February 08

Posted 14 July 2008 - 04:44 AM

STL Container: Queue

Queues... what the hell are they?
Queues are probably the most basic STL container. They don't really have too many commands, and they aren't very flexible. You can probably guess where they get their name from... think of a queue in a shop, they do about the same thing.

Starting our program
First off, we need to include the right stuff. All we need to include is #include <queue> and be using std::queue; but I'm gonna be using #include <iostream> just so that you can see what's happening.
#include <iostream>
#include <queue>

using namespace std;

int main ()
{

Creating a queue and adding values
Very simple to create, pretty much the same as any other STL creation. Also note that we use push() to add something to the queue... this does pretty much the same thing as push_back() would in a vector:
    queue <string> names; /* Declare a queue */
    names.push ("Danny"); /* Add some values to the queue */
    names.push ("Kayleigh"); /* Much like vectors */
    names.push ("Joe"); /* This basically does the same thing */
Simple, huh? Damn straight it is, and it doesn't get much harder, either.

Accessing the stuff in a queue
Really simple yet again, all we do in this next bit is make calls to 3 function, all of which are self explanatory:
  • size() returns the size of the queue (how many members are in it)
  • front() accesses the front element of the queue (the first one added)
  • back() accesses the back element of the queue (the last one added)
Notice the similarities to a queue in a shop? Basically, queue follows a "first come, first serve" pattern.
    cout << "There are currently " << names.size () << " people in the queue" << endl
         << "The person at the front of the queue is " << names.front () << endl
         << "The person at the back of the queue is " << names.back () << endl << endl;

Removing an element from a queue
Now, remember how I said it follows a "first come, first serve" rule? This is where you see it in action. The front member in the queue (currently me, Danny) is going to be first out of the queue, right? This is simple enough too:
    cout << names.front () << " has been served!" << endl;
    names.pop ();
    cout << "There are currently " << names.size () << " people in the queue" << endl
         << "The person at the front of the queue is " << names.front () << endl
         << names.back () << " is still at the back!" << endl;
Now, Danny (me) has been served and left the queue, so Kayleigh is now at the front, and Joe is still at the back.

Oh, and don't forget to close off main: }

And that's all there is to it! Told you it was simple!

Note: There is no such thing as an iterator when working with queues.

Here is all of the code that I used in writing this tutorial:
#include <iostream>
#include <queue>

using namespace std;

int main ()
{
    queue <string> names; /* Declare a queue */
    names.push ("Danny"); /* Add some values to the queue */
    names.push ("Kayleigh"); /* Much like vectors */
    names.push ("Joe"); /* This basically does the same thing */

    cout << "There are currently " << names.size () << " people in the queue" << endl
         << "The person at the front of the queue is " << names.front () << endl
         << "The person at the back of the queue is " << names.back () << endl << endl;

    cout << names.front () << " has been served!" << endl;
    names.pop ();
    cout << "There are currently " << names.size () << " people in the queue" << endl
         << "The person at the front of the queue is " << names.front () << endl
         << names.back () << " is still at the back!" << endl;
    cin.get ();
    return EXIT_SUCCESS;
}


For more information, visit the queue reference over at http://www.cplusplus.com :)

Is This A Good Question/Topic? 1
  • +

Replies To: STL queues

#2 Guest_Alex*


Reputation:

Posted 01 April 2010 - 07:01 PM

Dear author,

Did you test your source code?

error C2679: binary '<<' : no operator found which takes a right-hand operand of type 'std::basic_string<_Elem,_Traits,_Ax>' (or there is no acceptable conversion)
Was This Post Helpful? 0

#3 cdosrun1  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 16-October 13

Posted 16 November 2013 - 09:59 AM

View PostAlex, on 01 April 2010 - 07:01 PM, said:

Dear author,

Did you test your source code?

error C2679: binary '<<' : no operator found which takes a right-hand operand of type 'std::basic_string<_Elem,_Traits,_Ax>' (or there is no acceptable conversion)


Include the string header.

#include<string>

Was This Post Helpful? 0
  • +
  • -

#4 MCroz  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 05-February 14

Posted 21 February 2014 - 12:14 PM

can i ask a question sir?
with this queue thing

suppose we have a queue named name
example >> queue<string> name;

what if my name consist of 2 words
how can i input it in the queue including the whitespaces
because when im trying to use cin>>name or getline(cin,name) it gives me an error
not a compiler error but rather a logical error i think

here's my code though

#include <iostream>
#include <string>
#include <queue>
#include<conio.h>
using namespace std;

int main()
{

	queue<string> customer;
	queue<string> code;
	queue<int> numofbags;
	queue<float> weightofbags;
	string custname, cd;
	int i, ch, nb;
	float wob;

	while(i!=4)
    {
		cout<<"\n";
		cout<<"\t\t\t  - - Bag Handling System - - "<<endl;
		cout<<"\t- - By Aljon Alacapa, Paul Henston Ganut and Christian Morilla - - \n";
        cout<<"\n\t\t\t*************M E N U*************\n";
        cout<<"\n1.Enter Customer\t2.Release Customer\t3.Exit\n";
        cout<<"\n\nEnter option: ";
        cin>>ch;
        switch(ch)
        {
        case 1:
			system("cls");
			if (customer.size() >= 10)
			{
				cout<<"Queue OverFlow!";
				getch();
				system("cls");
				break;
			}
			else
			{
			cout<<"Enter Customer's Name: ";
			getline(cin,custname);
            customer.push(custname);
			cout<<"Enter Customer's BagCode: ";
			cin>>cd;
			code.push(cd);
			cout<<"Enter Number of Bags: ";
			cin>>nb;
			numofbags.push(nb);
			cout<<"Enter Weight of Bags: ";
			cin>>wob;
			weightofbags.push(wob);
			getch();
			system("cls");
			}
            break;
        case 2:
			system("cls");
			if (!customer.empty())
			{
				cout<<"The Customer with the following information can now retrieve his bag. \n";
				cout<<"Name: "<<customer.front()<<endl
					<<"Bag Code: "<<code.front()<<endl
					<<"Number of Bags: "<<numofbags.front()<<endl
					<<"Weight of Bags: "<<weightofbags.front()<<endl;
				customer.pop();
				code.pop();
				numofbags.pop();
				weightofbags.pop();
				getch();
				break;
			}
			else
			{
				cout<<"Queue Underflow!";
				getch();
				system("cls");
			}
            break;
        case 3:
            i=4;
            break;
        default :
            cout<<"\nEnter correct option\n";
			getch();
			system("cls");
            break;
        }
    }
	return 0;
}

Was This Post Helpful? 0
  • +
  • -

#5 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10573
  • View blog
  • Posts: 39,145
  • Joined: 27-December 08

Posted 21 February 2014 - 12:19 PM

MCroz- Please do not continue to spam your question across the site. You have an open thread here. Please use it.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1