Menu
Coddy logo textTech

復習 - ジェネリックコンテナ

CoddyのC++ジャーニー「オブジェクト指向プログラミング」セクションの一部。レッスン 69/104。

challenge icon

チャレンジ

簡単

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 し、10 の代わりに true または false を表示します。

    GenericContainer<T>& と任意の数の values を受け取り、fold expression を使用して各 value を container に push する、可変引数 function template pushAll を add します。

  • main.cpp:5つの inputs(それぞれ別の行)を読み取ります。
    1. First integer
    2. Second integer
    3. Third integer
    4. A double value
    5. string としての boolean(true または false

    次の操作を行って container を実演します。

    1. capacity 10 の GenericContainer<int> を Create する
    2. pushAll を使用して3つすべての integers を一度に add する
    3. size を表示する:Int container size: <size>
    4. printContainer を calls して integers を display する
    5. capacity 5 の GenericContainer<double> を Create する
    6. individual の push calls を使用して double value と literal 2.5 を push する
    7. printContainer を calls して doubles を display する
    8. capacity 5 の GenericContainer<bool> を Create する
    9. boolean input と literal false を push する
    10. printContainer を calls して booleans を display する(true/false が表示されます)

たとえば、inputs が 1020303.14true の場合:

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;
}

オブジェクト指向プログラミングのすべてのレッスン

自分で練習してみよう: C++オンラインコンパイラ