C++ IntArray

2021. 3. 19. 19:42·C++/Library

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
    */
저작자표시 (새창열림)

'C++ > Library' 카테고리의 다른 글

C++ std::exception  (0) 2021.03.22
C++ reference_wrapper  (0) 2021.03.21
C++ initializer_list  (0) 2021.03.19
C++ chrono  (0) 2021.03.16
C++ assert  (0) 2021.03.12
'C++/Library' 카테고리의 다른 글
  • C++ std::exception
  • C++ reference_wrapper
  • C++ initializer_list
  • C++ chrono
Caniro
Caniro
  • Caniro
    Minimalism
    Caniro
  • 전체
    오늘
    어제
    • 분류 전체보기 (317)
      • Algorithm (13)
        • 알기 쉬운 알고리즘 (10)
        • Search (1)
        • Sort (2)
      • Arduino (0)
      • C++ (185)
        • Class (46)
        • Exception (6)
        • Library (51)
        • Overloading (10)
        • SmartPointer (5)
        • Syntax (33)
        • TBC++ (23)
        • Templates (9)
        • VisualStudio (2)
      • Embedded (1)
      • Git (4)
      • Java (5)
      • Linux (16)
        • Error (1)
        • Linux Structure (11)
      • MacOS (7)
      • OS (1)
        • Concurrency (1)
      • Python (21)
        • Class (1)
        • Function (2)
        • Syntax (17)
      • Raspberrypi (9)
      • Review (1)
      • Utility (12)
        • VSCode (5)
        • VirtualBox (3)
      • Web (8)
        • Nginx (1)
        • React (3)
        • Django (1)
      • Windows (20)
        • Registry (3)
        • WSL (1)
        • DeviceDriver (6)
  • 블로그 메뉴

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

  • 공지사항

  • 인기 글

  • 태그

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

  • 최근 글

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

티스토리툴바