C++
 Computer >> コンピューター >  >> プログラミング >> C++

C++のSTLを使ったDeque(両端キュー)の実装方法を解説

両端キュー(Double Ended Queue、略称:Deque)は、キューの一種であり、先頭(front)と末尾(rear)の両端で要素の挿入・削除が行えるデータ構造です。通常のキューは片側から挿入し反対側から削除するだけですが、dequeは双方向からの操作に対応しているため、より柔軟なデータ管理が可能になります。

C++では、標準テンプレートライブラリ(STL)に <deque> ヘッダとして両端キューが標準搭載されているため、自前で実装しなくても手軽に利用できます。本記事では、STLのdequeを使用したメニュー形式の対話型プログラムを通じて、基本的な使い方を解説します。

アルゴリズム

プログラムの処理の流れは以下の通りです。

開始
   dequeコンテナとイテレータを宣言する。
   ユーザーの選択に応じて入力を受け取る。
   switch文の中で各操作に対応した関数を呼び出す:
   d.size()       :キューのサイズ(要素数)を返す。
   d.push_back()  :dequeの末尾に要素を追加する。
   d.push_front() :dequeの先頭に要素を追加する。
   d.pop_back()   :dequeの末尾から要素を取り除く。
   d.pop_front()  :dequeの先頭から要素を取り除く。
   d.front()      :dequeの先頭要素を返す。
   d.back()       :dequeの末尾要素を返す。
   dequeの全要素を表示する。
終了

使用する主なメンバー関数

関数説明
d.size()格納されている要素数を返します
d.push_back(x)末尾に要素 x を追加します
d.push_front(x)先頭に要素 x を追加します
d.pop_back()末尾の要素を削除します
d.pop_front()先頭の要素を削除します
d.front()先頭要素を参照します
d.back()末尾要素を参照します
d.begin() / d.end()走査用のイテレータを取得します

サンプルコード

#include<iostream>
#include <deque>
#include <string>
#include <cstdlib>
using namespace std;
int main() {
    deque<int> d;
    deque<int>::iterator it;
    int c, item;
    while (1) {
        cout<<"1.Size of the Deque"<<endl;
        cout<<"2.Insert Element at the End"<<endl;
        cout<<"3.Insert Element at the Front"<<endl;
        cout<<"4.Delete Element at the End"<<endl;
        cout<<"5.Delete Element at the Front"<<endl;
        cout<<"6.Front Element at Deque"<<endl;
        cout<<"7.Last Element at Deque"<<endl;
        cout<<"8.Display Deque"<<endl;
        cout<<"9.Exit"<<endl;
        cout<<"Enter your Choice: ";
        cin>>c;
        switch(c) {
            case 1:
                cout<<"Size of the Deque: "<<d.size()<<endl;
            break;
            case 2:
                cout<<"Enter value to be inserted at the end: ";
                cin>>item;
                d.push_back(item);
            break;
            case 3:
                cout<<"Enter value to be inserted at the front: ";
                cin>>item;
                d.push_front(item);
            break;
            case 4:
                item = d.back();
                d.pop_back();
                cout<<"Element "<<item<<" deleted"<<endl;
            break;
            case 5:
                item = d.front();
                d.pop_front();
                cout<<"Element "<<item<<" deleted"<<endl;
            break;
            case 6:
                cout<<"Front Element of the Deque: ";
                cout<<d.front()<<endl;
            break;
            case 7:
                cout<<"Back Element of the Deque: ";
                cout<<d.back()<<endl;
            break;
            case 8:
                cout<<"Elements of Deque: ";
                for (it = d.begin(); it != d.end(); it++)
                    cout<<*it<<" ";
                cout<<endl;
            break;
            case 9:
                exit(1);
            break;
            default:
                cout<<"Wrong Choice"<<endl;
        }
    }
    return 0;
}

実行結果

1.Size of the Deque
2.Insert Element at the End
3.Insert Element at the Front
4.Delete Element at the End
5.Delete Element at the Front
6.Front Element at Deque
7.Last Element at Deque
8.Display Deque
9.Exit

Enter your Choice: 1
Size of the Deque: 0

Enter your Choice: 2
Enter value to be inserted at the end: 1

Enter your Choice: 3
Enter value to be inserted at the front: 2

Enter your Choice: 6
Front Element of the Deque: 2

Enter your Choice: 7
Back Element of the Deque: 1

Enter your Choice: 1
Size of the Deque: 2

Enter your Choice: 8
Elements of Deque: 2 1

Enter your Choice: 2
Enter value to be inserted at the end: 4

Enter your Choice: 3
Enter value to be inserted at the front: 5

Enter your Choice: 8
Elements of Deque: 5 2 1 4

Enter your Choice: 4
Element 4 deleted

Enter your Choice: 5
Element 5 deleted

Enter your Choice: 8
Elements of Deque: 2 1

Enter your Choice: 9

解説と注意点

このプログラムは、無限ループ内でメニューを表示し、ユーザーが入力した選択肢に応じて switch 文で対応する処理へ分岐する構造になっています。実行結果からも分かるように、先頭への追加(5)、末尾への追加(4)が正しく反映され、Elements of Deque: 5 2 1 4 のように順序が保持されています。

注意: 空のdequeに対して front()back()pop_front()pop_back() を呼び出す動作は未定義です。実際の開発では、これらの操作の前に d.empty() で空かどうかを確認すると安全です。

  1. C++のSTLでset_intersectionを実装し、2つの集合の積集合を求める方法

    2つの集合の積集合(インターセクション)とは、両方の集合に共通して含まれる要素だけを集めたものです。set_intersection関数によってコピーされる要素は、必ず最初の集合から取り出され、元の順序がそのまま維持されます。また、この関数を正しく動作させるためには、処理前に両方の集合がそれぞれソート済みである必要があります。 集合に対する代表的な操作には、以下のようなものがあります。 和集合(ユニオン) 積集合(インターセクション) 対称差(排他的論理和・XOR) 差集合(減算) アルゴリズム Begin   結果を格納するvector型変数vとイテレータstを宣言する。   st =

  2. 【C++】STLのset_differenceを使って2つの集合の差分を求める方法

    2つの集合の「差(差集合)」とは、1つ目の集合には存在するが、2つ目の集合には存在しない要素だけから構成される集合のことです。set_difference関数によってコピーされる要素は、必ず1つ目の集合から取り出され、元の順序が保たれます。また、この関数を正しく動作させるためには、両方の集合があらかじめソート(整列)されている必要があります。代表的な集合演算には以下のようなものがあります。和集合(Union)積集合(Intersection)対称差(Symmetric Difference / 排他的論理和)差集合(Difference / 減算)アルゴリズムBegin 集合用のvec