C ++ practice: implementation of a lightweight array class

Source: Internet
Author: User

C ++ practice: implementation of a lightweight array class

Note: This array class can be regarded as a simplified version of vector in the standard library: supports general operations on arrays, supports copying and assigning values, and supports redefinition of sizes. multithreading is not considered, no extra space is pre-allocated for performance optimization, and no iterator is set.

# Include <iostream> // only used for output during testing. The array class itself does not need

Template <typename type>
Class Array
{
Public:
Typedef unsigned int size_tp; // array size (subscript) Type

Array (size_tp size = 0, type T = type (); // you can use size to specify the initial size, t to specify the Initial Value
Array (const array & array );
~ Array (); // automatic space release

Type & operator [] (size_tp index); // subscript access (read/write form)
Const type & operator [] (size_tp index) const; // subscript access (read-only)
Array & operator = (const array & RHs); // value assignment, change the size/Content

Size_tp get_size () const; // obtain the array size
Void resize (size_tp size); // reset the array size. If the size is small, the end data is discarded.

Void push_back (const type & value); // append an array element and increase the array size by one.

PRIVATE:
Void copy (const type * p_source, type * p_target, size_tp size );
Size_tp _ size;
Type * _ arr;
};

Template <typename type>
Array <type >:: array (size_tp size, type T): _ SIZE (size), _ Arr (size? New Type [size]: 0)
{
For (size_tp I = 0; I <size; ++ I) // not executed if (size = 0)
_ Arr [I] = T;
}

Template <typename type>
Array <type >:: array (const array & array ):
_ SIZE (array. _ size ),
_ Arr (array. _ size? New Type [array. _ SIZE]: 0)
{
Copy (_ array. _ arr, _ arr, array. _ size); // do nothing if (array. _ size = 0)
}

Template <typename type>
Array <type> ::~ Array ()
{
Delete [] _ arr;
}

Template <typename type>
Array <type> & array <type>: Operator = (const array <type> & RHs)
{
If (& RHS! = This)
{
Resize (RHS. _ size );
Copy (RHS. _ arr, _ arr, _ size );
}
Return * this;
}

Template <typename type>
Type & array <type >:: operator [] (size_tp index)
{
If (index> = _ SIZE)
Throw ("array: Out of range ");
Return _ arr [Index];
}

Template <typename type>
Const type & array <type>: operator [] (size_tp index) const
{
If (index> = _ SIZE)
Throw ("array: Out of range ");
Return _ arr [Index];
}

Template <typename type>
Array <type >:: size_tp array <type >:: get_size () const
{
Return _ size;
}

Template <typename type>
Void array <type >:: copy (const type * p_source, type * p_target, size_tp size)
{
For (size_tp I = 0; I <size; ++ I) // not executed if (size = 0)
P_target [I] = p_source [I];
}

Template <typename type>
Void array <type >:: resize (size_tp new_size)
{
If (new_size)
{
Type * P = new type [new_size];
Copy (_ arr, P, _ size <new_size? _ SIZE: new_size );
Delete [] _ arr;
_ Arr = P;
}
Else
{
Delete [] _ arr;
_ Arr = 0;
}
_ Size = new_size;
}

Template <typename type>
Void array <type >:: push_back (const type & value)
{
Resize (_ SIZE + 1 );
_ Arr [_ size-1] = value;
}

Int main () // main test function
{
Array <int> A (30, 5 );
Array <int> B;
B. push_back (20 );
B. push_back (100 );
A = B;
For (array <int >:: size_tp I = 0; I <A. get_size (); ++ I)
STD: cout <A [I] <STD: Endl;
Return 0;
}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.