C++/Queue Stack/queue

Материал из C\C++ эксперт
Перейти к: навигация, поиск

Put string into a queue

<source lang="cpp">

  1. include <iostream>
  2. include <string>
  3. include <queue>
  4. include <stack>

using namespace std; int main() {

  cout << "FIFO order:\n";
  queue<string> q;
  q.push("A");
  q.push("B");
  q.push("C");
  stack<string> s;
  while (q.size() > 0)
  {
     string name = q.front();
     q.pop();
     cout << name << "\n";
     s.push(name);
  }
  cout << "LIFO order:\n";
  while (s.size() > 0)
  {
     cout << s.top() << "\n";
     s.pop();
  }
  return 0;

}


 </source>


queue.front()

<source lang="cpp">

  1. include <iostream>

using std::cout; using std::endl;

  1. include <queue>

int main() {

  std::queue< double > values;
  
  values.push( 3.2 );
  values.push( 9.8 );
  values.push( 5.4 );
  cout << "Popping from values: ";
  
  while ( !values.empty() ) 
  {
     cout << values.front() << " ";
     values.pop();
  }
  cout << endl;
  return 0;

}

/* 

Popping from values: 3.2 9.8 5.4

*/       
   
   
 </source>


Queue of string: push(), empty(), front(), pop()

<source lang="cpp">

  1. include <iostream>
  2. include <string>
  3. include <queue>

using namespace std; int main() {

   queue<string> q;
   q.push( "A" );
   q.push( "B" );
   q.push( "C" );
   while ( !q.empty() ) {
       cout << q.front() << " ";
       q.pop();
    }
    return 0;

}


 </source>


Queue: push, pop and size

<source lang="cpp">


  1. include <iostream>
  2. include <queue>
  3. include <list>

using namespace std; int main() {

 int thedata[] = {45, 34, 56, 27, 71, 50, 62};
 queue<int, list<int> > q;  
 cout << "The queue size is now " << q.size() << endl;
 cout << "Pushing 4 elements " << endl;
 for (int i = 0; i < 4; ++i)
   q.push(thedata[i]);
 cout << "The queue size is now " << q.size() << endl;
 cout << "Popping 3 elements " << endl;
 for (int i = 0; i < 3; ++i) {
   cout << q.front() << endl;
   q.pop();
 }
 cout << "The queue size is now " << q.size() << endl;
 return 0;

}

/* 

The queue size is now 0 Pushing 4 elements The queue size is now 4 Popping 3 elements 45 34 56 The queue size is now 1

*/       
   
   
 </source>


queue: push, pop, front and size

<source lang="cpp">

/* The following code example is taken from the book

* "The C++ Standard Library - A Tutorial and Reference"
* by Nicolai M. Josuttis, Addison-Wesley, 1999
*
* (C) Copyright Nicolai M. Josuttis 1999.
* Permission to copy, use, modify, sell and distribute this software
* is granted provided this copyright notice appears in all copies.
* This software is provided "as is" without express or implied
* warranty, and with no claim as to its suitability for any purpose.
*/
  1. include <iostream>
  2. include <queue>
  3. include <string>

using namespace std; int main() {

   queue<string> q;
   // insert three elements into the queue
   q.push("These ");
   q.push("are ");
   q.push("more than ");
   // read and print two elements from the queue
   cout << q.front();
   q.pop();
   cout << q.front();
   q.pop();
   // insert two new elements
   q.push("four ");
   q.push("words!");
   // skip one element
   q.pop();
   // read and print two elements
   cout << q.front();
   q.pop();
   cout << q.front() << endl;
   q.pop();
   // print number of elements in the queue
   cout << "number of elements in the queue: " << q.size()
        << endl;

}

/* 

These are four words! number of elements in the queue: 0

*/       
   
   
 </source>


queue with doubles

<source lang="cpp">

  1. include <iostream>

using std::cout; using std::endl;

  1. include <queue>

int main() {

  std::queue< double > values;
  
  values.push( 3.2 );
  values.push( 9.8 );
  values.push( 5.4 );
  cout << "Popping from values: ";
  
  while ( !values.empty() ) 
  {
     cout << values.front() << " ";
     values.pop();
  }
  cout << endl;
  return 0;

} /* Popping from values: 3.2 9.8 5.4

*/
       
   
   
 </source>