Class template myarray-dynamic memory

Source: Internet
Author: User

Using a dynamic array to store data, there is the ability to insert, append, delete, and delete elements.

Using a linked list is a bit of a waste of space, with dynamic arrays adding delete and new operations.

The following code takes the form of a dynamic array.


Defines a class template myarray, which has the function of finding the array length, inserting elements, appending elements, deleting elements, finding elements, and validating them in main ().    #include <iostream> template<typename t>class myarray{private:t *a;    int Len;public:myarray (): A (NULL), Len (0) {} MyArray (int n): Len (n) {a = new t[n];}    MyArray (int n,t x): Len (n) {a = new t[n]; memset (a,x,sizeof (T) *n);}     ~myarray () {delete [] A;}    int size () const;    BOOL Insert (int i,t a);    BOOL Push_back (T a);    BOOL Find (T a);     BOOL Deletenum (int i);    T & operator[] (int i); void printall () const;}; Template<typename t> int myarray<t>::size () const{return len;} template<typename t> bool MyArray<    T>::find (T b) {for (int i=0;i<len;i++) if (b = = A[i]) return true; return false;}    Template<typename t> bool Myarray<t>::p ush_back (t b) {T *x = new t[len+1];    if (x = = NULL) return false;    for (int i=0;i<len;i++) x[i] = A[i];    X[len] = b;    delete [] A;    A = x;    len++; return true;} Template<tYpename t> bool Myarray<t>::insert (int i,t b) {if (i<0 | | i>len) return FALSE;    T *c = new T[len+1];    if (c = = NULL) return false;    Int J;    for (j=0;j<i;j++) c[j] = A[j];    C[j++] = b;    for (int k=j;k<len+1;k++) c[k] = a[k-1];    delete [] A;    A = C;    len++; return true;} Template<typename T>bool myarray<t>::d eletenum (int i) {if (i<0| |    I>=len) return false;    for (int j=i;j<len-1;j++) a[j] = a[j+1];    len--; return true;} Template<typename T>t & myarray<t>::operator[] (int i) {return a[i];} Template<typename t>void myarray<t>::p rintall () const{for (int i=0;i<len;i++) std::cout<<a[    i]<< ""; std::cout<< "\ n";}    int main () {myarray<int> A;    Using Std::cout;    Using Std::endl; Add 10,11,14 to Array,insert 0 to index 1 cout<< "add 10,11,14 to array by Order,insert 0 to index 1 area:" <    <endl; A.pUsh_back (10);    A.push_back (11);    A.push_back (14);    A.insert (1,0);     A.printall ();    Find, find if (a.find) cout<< "in array" <<endl;    else cout<< "in array" <<endl;    if (a.find) cout<< "in array" <<endl;     else cout<< "in array" <<endl;    Delete some num by index cout<< "delete index0 num:" <<endl;    A.deletenum (0);         A.printall ();    Get index i num cout<< "Print the index 1 num" <<endl;     cout<<a[1]<<endl;    System ("pause"); return 0;}

Class template myarray-dynamic memory

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.