The method of implementing sequential table in C + + _c language

Source: Internet
Author: User

Nonsense not to say more, directly to everyone on the key code.

#pragma once #include <assert.h> Template<class t> class Seqlist {public:seqlist (): _a (NULL), _size (1), _ca Pacity (1) {} seqlist (t* A, size_t size): _a (new t[size]), _size (size), _capacity (size) {for (size_t i = 0; i < _size;
++i) {_a[i] = a[i];} Copy constructor General notation/*seqlist (const seqlist<t>& s): _a (New T[s._size)), _size (s._size), _capacity (s._capacity) {for
(size_t i = 0; i < _size; ++i) _a[i] = S._a[i]; }*///Copy constructor modern notation Seqlist (const seqlist<t>& s): _a (NULL) {seqlist<t> tmp (s._a, s._size); Swap (_a, tmp._a)
;
_size = s._size;
_capacity = s._capacity; } ~seqlist () {if (_a) delete[] _a}//assignment operator Overloading General notation seqlist<t>& (const operator= s) {if (T He!= &s) {t* tmp = new t[s._size]; for (size_t i = 0; i < s._size; ++i) {tmp[i] = S._a[i]; delete[] _a =
tmp
_size = s._size;
_capacity = s._capacity;
return *this; }//assignment operator overload modern notation/*seqlist<t>& operator= (seqlist<t> s) {if (this!=; s) {swap (_a, s._a); _size = s._size; _capacity = s._capacity;} return *this;
}*/public:void Print () {for (size_t i = 0; i < _size; ++i) {cout<<_a[i]<< "";}
cout<<endl; } void Pushback (const t& x) {_checkcapacity (); _a[_size++] = x;} void Popback () {assert (_size > 0);--_size;} V OID Insert (int pos, const t& x) {assert (pos >= 0 && pos <= _size); _checkcapacity (); int iindex = _size
;
while (Iindex > POS)//int and size_t comparison why not? 
{_a[iindex] = _a[iindex-1];
--iindex;
} _a[iindex] = x;
++_size;
} void Erase (size_t pos) {assert (_size > 0 && pos < _size); size_t index = pos; while (Index < _size-1)
{_a[index] = _a[index+1]; ++index}
--_size;
int find (const t& x) {for (size_t i = 0; i < _size; ++i) {if (_a[i] = = x) {return i}} return-1;} t& operator[] (size_t index) {assert (index >= 0 && Index < _size); return _a[index];} void Reserve (size _t size)//reserve space, increase to size {_capacity = siZe
_a = (t*) realloc (_a, _capacity * sizeof (T)); void clear ()//Do not free space {_size = 0;} void size () {return _size} protected:void _checkcapacity () {if _size+1 > _c
apacity) {_capacity = _capacity*2; _a = (t*) realloc (_a, _capacity * sizeof (T));}
protected:t* _a;
size_t _size;
size_t _capacity; };

The above is a small set to introduce the sequential table of C + + implementation method, I hope to help you, if you have any questions please give me a message, small series will promptly reply to everyone. Here also thank you very much for the cloud Habitat Community website support!

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.