C + + language implementation of linear table array instance _c language

Source: Internet
Author: User

This article illustrates an array of C + + language implementations of linear tables. Share to everyone for your reference. The specific analysis is as follows:

It is more reasonable and convenient to describe some data structures with the features of constructors and destructors in C + +. There is a problem, however, that the compiler does not support separate compilation of templates, which is uncomfortable

#include <iostream> using namespace std;
 Template<class t> class CArray {public:carray (const int &imax);
 CArray ();
 ~carray ();
 void Create (const int &imax);
 void Destroy ();
 void Print ();
 BOOL IsEmpty ();
 BOOL Isfull ();
 void Append (const T &data);
 int GetLength ();
 int Getmax ();
 BOOL Delete (const int &pos);
 BOOL Insert (const int &pos,const T &data);
void operator+= (const T &data);
 Private:t *m_parray;
 int M_len;
 int M_max;
void Reset ();
}; Template<class t> carray<t>::carray (const int &imax) {Create (IMax);} template<class t> CArray&lt ; T>::~carray () {Destroy ():} template<class t> void carray<t>::create (const int &imax) {M_parray = n
 EW T[imax];
 M_max = IMax;
 M_len = 0;
memset (m_parray,0,sizeof (M_parray)); Template<class t> void Carray<t>::D Estroy () {delete [] m_parray;} template<class t> void carray< T&gt::P rint () {if (IsEmpty ()) {cout<< "no data!
 "<<endl;
  else {for (int ix =0; IX < M_len; ++ix) {cout<<m_parray[ix]<< ",";
 } cout<<endl;
 } template<class t> bool Carray<t>::isempty () {if (0 = M_len) {return true;
 else {return false;
  } template<class t> bool Carray<t>::isfull () {if (M_len = = M_max) {Reset ();
 return false;
 else {return false; }} template<class t> void carray<t>::append (const T &data) {if (!
  Isfull ()) {++m_len;
 M_PARRAY[M_LEN-1] = data; } template<class t> int carray<t>::getlength () {return m_len;} template<class t> bool CARRAY&LT;T&G
  t;::D elete (const int &pos) {if (pos > M_len | | pos <= 0) {cout<< "location not valid" <<endl;
 return false;
 for (int ix = Pos-1 IX < m_len-1 + + ix) {M_parray[ix] = M_parray[ix + 1];
 }--m_len;
return true; } template<class t> void carray<t>::operator+= (const T &data) {this->append (data); } template<class t> bool Carray<t>::insert (const int &pos,const T &data) {if (Isfull ()) {return F
 Alse;  
  else {for (int ix = m_len-1 IX >= pos-1;--IX) {M_parray[ix + 1] = M_parray[ix];
  } m_parray[pos-1] = data;
  ++m_len;
 return true; } template<class t> Carray<t>::carray () {Create (5);} template<class t> void Carray<t>::reset (
 {T *pt = new T[m_max * 2];
 memset (pt,0,sizeof (PT));
 for (int ix = 0; ix < M_len; + + ix) {Pt[ix] = M_parray[ix];
 
 } delete [] M_parray;
 M_parray = PT;
M_max = M_max * 2; Template<class t> int Carray<t>::getmax () {return m_max;}

I hope this article will help you with your C + + programming.

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.