C++標準テンプレートライブラリSTLキューqueueの使用方法と応用紹介(一)


queue
Queueテンプレートクラスの定義はヘッダファイルにあります.
stackテンプレートクラスと似ていますが、queueテンプレートクラスにも2つのテンプレートパラメータが必要です.1つは要素タイプ、1つはコンテナタイプ、要素タイプは必要です.コンテナタイプはオプションで、デフォルトはdequeタイプです.
queueオブジェクトを定義するコードの例は次のとおりです.
queue q1;
queue q2;
Queueの基本的な操作は、次のとおりです.
例:q.push(x);xをキューの末端に接続します.
例:q.pop();キューの最初の要素をポップアップします.ポップアップされた要素の値は返されません.
例:q.front()、すなわち最も早くキューに押し込まれた要素などのキューヘッダ要素にアクセスします.
例:q.back()のようなキューの最後の要素にアクセスします.つまり、最後にキューに押し込まれた要素です.
例:q.empty()のようにキューが空であると判断し、キューが空であるとtrueを返す.
例:q.size()などのアクセスキュー内の要素の数
std::queue
FIFO queue
queues are a type of container adaptor, specifically designed to operate in a FIFO context (first-in first-out), where elements are inserted into one end of the container and extracted from the other.
queues are implemented as
containers adaptors, which are classes that use an encapsulated object of a specific container class as its
underlying container, providing a specific set of member functions to access its elements. Elements are
pushed into the
"back"of the specific container and
popped from its
"front".
The underlying container may be one of the standard container class template or some other specifically designed container class. The only requirement is that it supports the following operations:
  • front()
  • back()
  • push_back()
  • pop_front()

  • Therefore, the standard container class templates deque and list can be used. By default, if no container class is specified for a particular queue class, the standard container class template deque is used.
    In their implementation in the C++ Standard Template Library, queues take two template parameters:
     
    template < class T, class Container = deque<T> > class queue;

    Where the template parameters have the following meanings:
  • T: Type of the elements.
  • Container: Type of the underlying container object used to store and access the elements.

  • In the reference for the queue member functions, these same names are assumed for the template parameters.
     
    #include <iostream>
    #include <queue>
    #include <string>
    using namespace std;
    void test_empty()
    {
      queue<int> myqueue;
      int sum (0);
    
      for (int i=1;i<=10;i++) myqueue.push(i);
    
      while (!myqueue.empty())
      {
         sum += myqueue.front();
         myqueue.pop();
      }
        cout << "total: " << sum << endl;
    }//    : total: 55
    void test_pop()
    {
      queue<int> myqueue;
      int myint;
    
      cout << "
    Please enter some integers (enter 0 to end):
    "; do { cin >> myint; myqueue.push (myint); } while (myint); cout << "myqueue contains: "; while (!myqueue.empty()) { cout << " " << myqueue.front(); myqueue.pop(); } } /******** : Please enter some integers (enter 0 to end): 512 605 420 517 532 0 myqueue contains: 512 605 420 517 532 0 ********/ void test_size() { queue<int> myints; cout << "0. size: " << (int) myints.size() << endl; for (int i=0; i<5; i++) myints.push(i); cout << "1. size: " << (int) myints.size() << endl; myints.pop(); cout << "2. size: " << (int) myints.size() << endl; } /**** : 0. size: 0 1. size: 5 2. size: 4 ****/ int main() { test_empty(); cout<<"
    ***********************************************
    "; test_size(); cout<<"
    ***********************************************
    "; test_pop(); cout<<"
    ***********************************************
    "; queue<string> q; // insert three elements into the queue q.push("These "); q.push("are "); q.push("more than "); //cout << "number of elements in the queue: " << q.size()<< endl; // read and print two elements from the queue cout << q.front(); q.pop(); cout << q.front(); q.pop(); //cout << "number of elements in the queue: " << q.size()<< endl; // insert two new elements q.push("four "); q.push("words!"); //cout << "
    number of elements in the queue: " << q.size()<< endl; // 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; } /******* * : total: 55 *********************************************** 0. size: 0 1. size: 5 2. size: 4 *********************************************** Please enter some integers (enter 0 to end): 512 605 420 517 532 0 myqueue contains: 512 605 420 517 532 0 *********************************************** These are four words! number of elements in the queue: 0 Process returned 0 (0x0) execution time : 33.512 s Press any key to continue. ********/