STLのC++のdeque_resize()
与えられているのは、C ++STLのdequeresize()関数の機能を表示するタスクです。
Dequeとは
Dequeは、両端で拡張と縮小の機能を提供するシーケンスコンテナである両端キューです。キューデータ構造により、ユーザーはENDでのみデータを挿入し、FRONTからデータを削除できます。バス停のキューを例にとると、ENDからのみキューに挿入でき、FRONTに立っている人が最初に削除されますが、両端キューではデータの挿入と削除が両方で可能です。終わり。
deque resize()関数とは
deque resize()関数は、dequeのサイズを変更するために使用されます。サイズが現在のサイズより大きい場合、新しい要素が両端キューの最後に挿入されます。指定されたサイズが現在のサイズよりも小さい場合、余分な要素は削除されます。
構文
dequename.resize(n)
dequename.resize(n)
n:両端キューのサイズを定義します
例
入力 現在のサイズ-5
Deque − 12 13 14 15 16
出力 サイズ変更後のサイズ-7
New Deque − 11 12 13 14 15 16 17
入力 現在のサイズ-5
Deque − F O R C E
出力 サイズ変更後のサイズ-4
New Deque − F O R C
アプローチに従うことができます
-
まず、両端キューを宣言します。
-
次に、両端キューのサイズを確認します。
-
次に、両端キューを印刷します。
-
次に、resize()関数を定義します
-
次に、サイズ変更後に新しい両端キューを印刷します。
上記のアプローチを使用することで、両端キューのサイズを変更できます。
例
/ / C++ code to demonstrate the working of deque resize( ) function
#include <iostream.h>
#include<deque.h>
Using namespace std;
int main ( ){
// initializing the deque
Deque<int> deque = { 85, 87, 88, 89, 90 };
cout<< “ Size of deque” << deque.size( )<< “\n”;
// print the deque
cout<< “ Deque: “;
for( auto x = deque.begin( ); x != deque.end( ); ++x)
cout<< *x << “ “;
// defining the resize( ) function
deque.resize(7);
// printing deque after resize
cout<< “Deque after resize” << deque.size( ) <<”\n”;
cout<< “ New Deque:”;
for( x = deque.begin( ) ; x != deque.end( ); ++x)
cout<< “ “ <<*x;
return 0;
} 出力
上記のコードを実行すると、次の出力が生成されます
Input - Size of deque: 5 Deque: 85 87 88 89 90 Output - Deque after resize: 7 New Deque: 85 87 88 89 90 0 0
例
/ / C++ code to demonstrate the working of deque resize( ) function
#include <iostream.h>
#include<deque.h>
Using namespace std;
int main( ){
/ / initializing deque
deque<int> deque ={ 14, 15, 16, 17, 18, 19, 20 };
cout<< “ Size of deque” << deque.size( )<< “\n”;
/ / print the deque
cout<< “ Deque: “;
for( auto x = deque.begin( ); x != deque.end( ); ++x)
cout<< *x << “ “;
/ / defining the resize( ) function
deque.resize(5);
/ / printing deque after resize
cout<< “Deque after resize” << deque.size( ) <<”\n”;
cout<< “ New Deque:”;
for( x = deque.begin( ) ; x != deque.end( ); ++x)
cout<< “ “ <<*x;
return 0;
} 出力
上記のコードを実行すると、次の出力が生成されます
Input: Size of deque: 7 Deque:14 15 16 17 18 19 20 Output: Deque after size: 5 New Deque: 14 15 16 17 18
-
C ++ STL(3.5)でスタック
C ++ STLでは、スタックはLIFO構造として実装されるコンテナーとして使用されます。 LIFOは後入れ先出しを意味します。 Stackは、本が上下に並べられた本の山と見なすことができ、最後に挿入された本が最初に削除されるため、LIFO構造と呼ばれます。 スタックに関連付けられている操作は- Top() -この関数は、スタックの最上位要素への参照を返します。 構文 --name_of_stack.top() パラメータ -パラメータなし 戻り値 -スタックコンテナの最上位要素への参照 Push() -この関数は、要素をスタックコンテナに挿入するために使用されま
-
STLでDequeを実装するC++プログラム
Double Ended Queueは、挿入および削除操作が両端(前面と背面)で実行されるキューデータ構造です。データは前部と後部の両方の位置に挿入でき、前部と後部の両方の位置から削除できます。 アルゴリズム Begin Declare deque vector and iterator. Take the input as per choice. Call the functions within switch operation: d.size() = Returns the size of queue. d.push_back() = It is used