Menu
Coddy logo textTech

요약 - 제네릭 컨테이너

Coddy C++ 여정의 객체 지향 프로그래밍 섹션에 포함된 레슨. 104개 중 69번째.

challenge icon

챌린지

쉬움

이제 이 장에서 템플릿에 대해 배운 모든 내용을 하나로 모으는 GenericContainer 클래스 템플릿을 만들어 보겠습니다. 템플릿 기법을 통해 요소를 동적으로 저장하고 유용한 연산을 제공하는 유연하고 타입 안전한 컨테이너를 만들게 됩니다.

코드는 두 개의 파일로 구성합니다.

  • GenericContainer.h: 여기에서 클래스 템플릿을 Define하세요. GenericContainer는 다음을 수행해야 합니다.

    배열에 대한 포인터를 사용하여 요소를 동적으로 저장하고, current 크기와 capacity를 함께 추적합니다.

    다음 핵심 메서드를 제공합니다.

    • initial capacity를 받는 Constructor
    • 동적 메모리를 올바르게 정리하는 destructor
    • push(T value): 컨테이너에 요소를 add합니다(이 challenge에서는 capacity가 항상 충분하다고 가정합니다).
    • get(size_t index): 지정된 index의 요소를 반환합니다.
    • getSize(): 현재 요소 수를 반환합니다.

    GenericContainer<T>&를 받고 모든 요소를 공백으로 구분하여 출력한 뒤 newline을 출력하는 printContainer라는 function template을 추가합니다.

    GenericContainer<bool>에 대한 printContainer의 template specialization을 Create하여 1 또는 0 대신 true 또는 false를 출력하도록 합니다.

    GenericContainer<T>&와 임의 개수의 값을 받아 fold expression을 사용하여 각 값을 컨테이너에 push하는 가변 인자 function template pushAll을 추가합니다.

  • main.cpp: 다섯 개의 input을 읽습니다(각각 별도의 줄에 입력).
    1. 첫 번째 정수
    2. 두 번째 정수
    3. 세 번째 정수
    4. double 값
    5. 문자열로 입력된 boolean(true 또는 false)

    다음과 같이 컨테이너를 사용합니다.

    1. capacity가 10인 GenericContainer<int>를 Create합니다.
    2. pushAll을 사용하여 세 정수를 한 번에 모두 add합니다.
    3. 크기를 출력합니다: Int container size: <size>
    4. printContainer를 호출하여 정수를 display합니다.
    5. capacity가 5인 GenericContainer<double>를 Create합니다.
    6. 개별 push calls를 사용하여 double 값과 literal 2.5를 push합니다.
    7. printContainer를 호출하여 doubles를 display합니다.
    8. capacity가 5인 GenericContainer<bool>를 Create합니다.
    9. boolean input과 literal false를 push합니다.
    10. printContainer를 호출하여 booleans를 display합니다(true/false가 표시되어야 합니다).

예를 들어 input이 10, 20, 30, 3.14, true인 경우:

Int container size: 3
10 20 30 
3.14 2.5 
true false 

이 challenge에서는 컨테이너를 위한 클래스 템플릿, 사용자 지정 bool 출력을 위한 template specialization, 그리고 유연한 pushAll function을 위한 가변 인자 템플릿을 함께 사용합니다. 컨테이너는 RAII 원칙에 따라 자체 메모리를 관리하며, 템플릿이 올바른 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을 사용하여 세 개의 정수를 한 번에 추가
    
    // 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++ 컴파일러