C++ IntArray

2021. 3. 19. 19:42·SW개발/C++
반응형

IntArray

  • int 자료형을 여러개 담을 수 있는 컨테이너

    #include <iostream>
    
    class IntArray
    {
        int*        data_ = nullptr;
        unsigned    length_ = 0;
    
    public:
        IntArray(unsigned length = 0)
        {
            initialize(length);
        }
    
        IntArray(const std::initializer_list<int>& list)
        {
            initialize(list.size());
            int count = 0;
            for (auto& e : list)
                data_[count++] = e;
        }
    
        ~IntArray()
        {
            delete[] data_;
        }
    
        void    initialize(unsigned length)
        {
            length_ = length;
            if (length_ > 0)
                data_ = new int[length_];
        }
    
        void    reset()
        {
            if (data_)
            {
                delete[] data_;
                data_ = nullptr;
            }
            length_ = 0;
        }
    
        void    resize(const unsigned& length)
        {
            if (length == 0)
                reset();
            else if (length_ < length)
            {
                int* new_data_ = new int[length];
                for (unsigned i = 0; i < length_; ++i)
                    new_data_[i] = data_[i];
                delete[] data_;
                data_ = new_data_;
            }
            length_ = length;
        }
    
        void    insertBefore(const int& value, const int& ix)
        {
            resize(length_ + 1);
            for (int i = length_ - 2; i >= ix; --i)
                data_[i + 1] = data_[i];
            data_[ix] = value;
        }
    
        void    remove(const int& ix)
        {
            for (int i = ix; i < length_ - 1; ++i)
                data_[i] = data_[i + 1];
            --length_;
            if (length_ <= 0)
                reset();
        }
    
        void    push_back(const int& value)
        {
            insertBefore(value, length_);
        }
    
        friend std::ostream& operator << (std::ostream& out, const IntArray& arr)
        {
            for (unsigned i = 0; i < arr.length_; ++i)
                out << arr.data_[i] << ' ';
            return out;
        }
    };
    
    int        main()
    {
        using namespace std;
    
        IntArray my_arr{ 1, 3, 5, 7, 9 };
        cout << my_arr << endl;
    
        my_arr.insertBefore(10, 1);
        cout << my_arr << endl;
    
        my_arr.remove(3);
        cout << my_arr << endl;
    
        my_arr.push_back(13);
        cout << my_arr << endl;
    }
    
    /* stdout
    1 3 5 7 9
    1 10 3 5 7 9
    1 10 3 7 9
    1 10 3 7 9 13
    */
반응형
저작자표시 (새창열림)

'SW개발 > C++' 카테고리의 다른 글

C++ 상속 (Inheritance)  (0) 2021.03.19
따라하며 배우는 C++ 11장  (0) 2021.03.19
C++ 의존 관계 (Dependency)  (0) 2021.03.19
C++ 연계, 제휴 관계 (Association)  (0) 2021.03.19
C++ 구성 관계 (Composition Relationship)  (0) 2021.03.19
'SW개발/C++' 카테고리의 다른 글
  • C++ 상속 (Inheritance)
  • 따라하며 배우는 C++ 11장
  • C++ 의존 관계 (Dependency)
  • C++ 연계, 제휴 관계 (Association)
Caniro
Caniro
  • Caniro
    Minimalism
    Caniro
  • 전체
    오늘
    어제
    • 전체보기 (319)
      • SW개발 (268)
        • Java Spring (6)
        • C++ (186)
        • Python (21)
        • Linux (16)
        • 알고리즘 (13)
        • Git (4)
        • Embedded (1)
        • Raspberrypi (9)
        • React (3)
        • Web (2)
        • Windows Device Driver (6)
      • IT(개발아님) (46)
        • Windows (26)
        • MacOS (7)
        • Utility (11)
      • 챗봇 짬통 (0)
      • 일상 (2)
  • 블로그 메뉴

    • 홈
    • 태그
    • 방명록
  • 링크

  • 공지사항

  • 인기 글

  • 태그

    java
    MacOS
    스프링 프레임워크 핵심 기술
    citrix workspace
    EXCLUDE
    윈도우
    백기선
    windows
    KakaoTalk
    윈도우 명령어
    알림
    시스템 복구
    맥북 카카오톡 알림 안뜸
    spring
    Solaris 10
    mspaint
    SunOS 5.1
    제외
    unix
    dism
    Windows 11
    logi options
    vscode
    스프링
    Workspace
    로지텍 마우스 제스처
    그림판
    SFC
  • 최근 댓글

  • 최근 글

  • hELLO· Designed By정상우.v4.10.3
Caniro
C++ IntArray
상단으로

티스토리툴바