復習 - ジェネリックコンテナ
CoddyのC++ジャーニー「オブジェクト指向プログラミング」セクションの一部。レッスン 69/104。
チャレンジ
簡単GenericContainer class template を構築して、この章で学んだ templates に関するすべてを組み合わせましょう。template のテクニックを通じて、elements を dynamic に格納し、便利な操作を提供できる、柔軟で type-safe な container を作成します。
コードを2つのファイルに分けて整理します。
GenericContainer.h:ここで class template を Define します。GenericContainerは次の要件を満たす必要があります。pointer to an array を使用して elements を dynamic に格納し、current size と capacity も追跡します。
次の core methods を提供します。
- initial capacity を受け取る constructor
- dynamic memory を適切に cleans する destructor
push(T value):container に element を add します(この challenge では capacity は常に十分であると仮定します)get(size_t index):指定された index の element を返しますgetSize():現在の elements 数を返します
GenericContainer<T>&を受け取り、すべての elements を spaces で区切って表示し、その後に newline を出力する、printContainerという function template を add します。GenericContainer<bool>用にprintContainerの template specialization を Create し、1や0の代わりにtrueまたはfalseを表示します。GenericContainer<T>&と任意の数の values を受け取り、fold expression を使用して各 value を container に push する、可変引数 function templatepushAllを add します。main.cpp:5つの inputs(それぞれ別の行)を読み取ります。- First integer
- Second integer
- Third integer
- A double value
- string としての boolean(
trueまたはfalse)
次の操作を行って container を実演します。
- capacity 10 の
GenericContainer<int>を Create する pushAllを使用して3つすべての integers を一度に add する- size を表示する:
Int container size: <size> printContainerを calls して integers を display する- capacity 5 の
GenericContainer<double>を Create する - individual の
pushcalls を使用して double value と literal2.5を push する printContainerを calls して doubles を display する- capacity 5 の
GenericContainer<bool>を Create する - boolean input と literal
falseを push する printContainerを calls して booleans を display する(true/falseが表示されます)
たとえば、inputs が 10、20、30、3.14、true の場合:
Int container size: 3
10 20 30
3.14 2.5
true false この challenge では、container 用の class templates、custom bool printing 用の template specialization、そして柔軟な pushAll function 用の variadic templates を組み合わせています。container は RAII principles に従って自身の memory を管理し、templates が適切な resource management とどのように統合されるかを示します。
自分で試してみよう
#include <iostream>
#include <string>
#include "GenericContainer.h"
using namespace std;
int main() {
// 入力を読み取る
int num1, num2, num3;
double doubleVal;
string boolStr;
cin >> num1;
cin >> num2;
cin >> num3;
cin >> doubleVal;
cin >> boolStr;
// 文字列をboolに変換する
bool boolVal = (boolStr == "true");
// TODO: 容量10のGenericContainer<int>を作成する
// TODO: pushAllを使用して3つの整数を一度に追加する
// TODO: Print the size: "Int container size: <size>"
// TODO: Call printContainer to display the integers
// TODO: 容量5のGenericContainer<double>を作成する
// TODO: 個別のpush呼び出しを使用してdouble値と2.5をプッシュする
// TODO: Call printContainer to display the doubles
// TODO: 容量5のGenericContainer<bool>を作成する
// TODO: ブール入力とfalseをプッシュする
// TODO: Call printContainer to display the booleans
return 0;
}
オブジェクト指向プログラミングのすべてのレッスン
3コンストラクタとデストラクタ
デフォルトコンストラクタパラメータ付きコンストラクタコピーコンストラクタムーブコンストラクタコンストラクタ初期化リスト委譲コンストラクタデストラクタ徹底解説Rule of Three / Five / Zero復習 - Stringクラス6演算子オーバーロード
演算子オーバーロード入門算術演算子のオーバーロード比較演算子のオーバーロードストリーム演算子代入演算子のオーバーロード[] と () の演算子オーバーロード型変換演算子復習 - Matrixクラス自分で練習してみよう: C++オンラインコンパイラ