Array (C ++ implementation, second edition)

xiaoxiao2021-03-06  42

#include #include #include #include usingspace std;

namespace mylib {template > class array {public: // type defined typedef Ty value_type; typedef size_t size_type; typedef ptrdiff_t diffecence_type; typedef Ty * pointer; typedef const Ty * const_pointer TypedEf Ty & Reference; typef const same & const_reference; // Constructs function set array (size_type sz = 0, allocator a = allocator (): Alloc (a), _size (sz), IA (NULL) {GET_MEMORY (); STDE :: uninitialized_fill_n (ia, _size, 0);} Array (const array & coll): Alloc (coll.alloc), _size (coll.size ()), IA (NULL) {get_memory (); std: : uninitialized_copy (coll.begin (), coll.end (), ia);} array (const_pointer cap, const size_type sz, allocator a = allocator ()): Allocator ()): Allocator (), _Size (SZ), IA (NULL) {GET_MEMORY (); std :: uninitialized_copy (CAP, & CAP [SZ-1], IA);} ~ array (void) {for (size_type i = 0; i <_size; i) {alloc.dest Roy (& IA [i]);} alloc.deallocate (} // operator overload set const bool operator == (const array & coll) const {ix (_size! = Coll.size () Return false; for (size_type i = 0; i <_size; i) {IF (IA [i]! = COLL [I]) Return False;} return true;} const boater! = (Const Array < TY> & coll) const {ix (_Size! = Coll.size ()) Return true; for (size_type i = 0; i <_size; i) {IF (IA [i]! = COLL [I]) Return true;} regn false;} const array & operator = (const array & coll) {= == = (t ==)

COLL) RETURN * this; if (IA! = null) {for (size_type i = 0; i <_size; i) Alloc.DESTRO (& IA [i]); alloc.deallocate (IA, _size);} _size = coll.size (); get_memory (); std :: uninitialized_copy (coll.begin (), coll.end (), ia); return coll;} reference Operator [] (const size_type index) {Return IA [index] ;} const_reference operator [] (const size_type index) const {return ia [index];} // built-in method const_pointer begin (void) const {return ia;} const_pointer end (void) const {return & ia [_size-1]; } const size_type size (void) const {return _size;} const value_type min (void) const {return * std :: min_element (ia, & ia [_size-1]);} const value_type max (void) const {return * std :: MAX_EEMENT (IA, & IA [_Size-1]);

PRIVATE: // Private function void get_memory (void) {= alloc.allocate (_size);}

// Private data Allocator Alloc; Pointer IA; SIZE_TYPE _SIZE;};

INT main (void) {using mylib :: array; ostream_iterator outit (cout, "/ t"); int A [] = {1, 5, 4, 8, 5, 6}; array Collx (10); Array Colly (a, sizeof (a) / sizeof (a [0))); COPY (COLLX.EGIN (), COLLX.END (), OUTIT); COUT << Endl; for Size_t i = 0; i

转载请注明原文地址:https://www.9cbs.com/read-78951.html

New Post(0)