VasilM

example_dynamic_container

Apr 1st, 2013
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.98 KB | None | 0 0
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. class Tst {
  5.     int i;
  6.     double d;
  7.     static int n;
  8. public :
  9.     Tst() {
  10.         i = ++n;
  11.         d = 10*rand()/RAND_MAX;
  12.     }
  13.     Tst( int i, double d ):i(i),d(d){}
  14.     bool operator<(const Tst& b)const {
  15.         return d < b.d;
  16.     }
  17.     bool operator==(const Tst& b)const {
  18.         return d == b.d;
  19.     }
  20.     ostream& ins(ostream& os) {
  21.         os<< i << " " << d << endl;
  22.         return os;
  23.     }
  24. };
  25. int Tst::n = 0;
  26. ostream& operator<<(ostream& os, Tst& x) {
  27.     return x.ins( os );
  28. }
  29.  
  30. template <class T> class DArray {
  31.     unsigned sz, cap;
  32.     T *r;
  33. public:
  34.     class IndexError {
  35.         int r, i;
  36.     public:
  37.         IndexError(int r, int i):r(r), i(i) {}
  38.         ostream& rep(ostream& os) {
  39.             os<<"Index= "<< i <<"should be >=0 and <"<< r << endl;
  40.             return os;
  41.         }
  42.     };
  43.  
  44.     class iterator;
  45.     friend class iterator;
  46.     class iterator {
  47.         unsigned index;
  48.         DArray &p;
  49.     public:
  50.         iterator(DArray& p, unsigned index =0):p(p),index(index) {}
  51.         T& operator*(){ return p.r[index]; }
  52.         iterator& operator++() {
  53.             if(index<p.sz) index++;
  54.             return *this;
  55.         }
  56.         iterator& operator++(int) {
  57.             iterator x(p, index);
  58.             if(index<p.sz) index++;
  59.             return x;
  60.         }
  61.         bool operator==(const iterator& q) {
  62.             return index == q.index;
  63.         }
  64.         bool operator!=(const iterator& q) {
  65.             return index != q.index;
  66.         }
  67.     };
  68.     iterator begin(){ return iterator(*this); }
  69.     iterator end(){ return iterator(*this, sz); }
  70.  
  71.     DArray(unsigned n=1):cap(n),sz(n) {
  72.         r = new T[n];
  73.     }
  74.     DArray(const DArray& a) {
  75.         cap = a.cap; sz = a.sz;
  76.         r = new T[sz];
  77.         for(unsigned i; i<sz; i++) r[i] = a.r[i];
  78.     }
  79.     DArray& operator=(const DArray& right) {
  80.         if(this!=&right) {
  81.             delete [] r;
  82.             cap = a.cap; sz = a.sz;
  83.             r = new T[sz];
  84.             for(unsigned i; i<sz; i++) r[i] = a.r[i];
  85.         }
  86.         return *this;
  87.     }
  88.     virtual ~DArray() { delete [] r; }
  89.  
  90.     T& operator[](int index) {
  91.         if( index < 0 || index >= sz ) {
  92.             throw IndexError( sz, index );
  93.         }
  94.         return r[index];
  95.     }
  96.     void ensureCapacity( unsigned newCap ) {
  97.         if( newCap > cap ) {
  98.             T *b = new T[newCap];
  99.             for(unsigned i; i<sz; i++) b[i] = r[i];
  100.             delete []r;
  101.             r = b;
  102.             cap = newCap;
  103.         }
  104.     }
  105.     unsigned size() { return sz; }
  106.     void add( T& e ) {
  107.         if(sz == cap)
  108.             ensureCapacity( 2+(unsigned)(1.1*cap));
  109.         r[sz++] = e;
  110.     }
  111.     void removeFromPos( int pos ) {
  112.         if( pos>=0 && pos<sz ) {
  113.             for( unsigned i=pos-1; i<sz; i++ ) r[i-1] = r[i];
  114.             sz--;
  115.         }
  116.     }
  117.     int indexOf( T& e) {
  118.         for(unsigned i; i<sz; i++)
  119.             if( e == r.[i]) return i;
  120.         return -1;
  121.     }
  122.     void insertAt( T& e, unsigned pos ) {
  123.         if(sz == cap)
  124.             ensureCapacity(2 +(unsigned)(1.1*cap));
  125.         for(unsigned i=sz-1; i>=pos; i--) r[i+1] = r[i];
  126.         r[pos] = e;
  127.         sz++;
  128.     }
  129.     void forEach( void(*f)(T &)) {
  130.         for(unsigned i=0; i<sz; i++) f(r[i]);
  131.     }
  132. };
  133.  
  134. template< class T> void print( T& x) {
  135.     cout << x << endl;
  136. }
  137.  
  138. int main() {
  139.     int n;
  140.     cin>> n;
  141.     DArray<Tst> a(n);
  142.     a.forEach( print<Tst>);
  143.     cout << endl;
  144.     DArray<Tst>::iterator p=a.begin();
  145.     for(; p!=a.end(); p++ ) {
  146.         cout << *p << endl;
  147.     }
  148.     return 0;
  149. }
Advertisement
Add Comment
Please, Sign In to add comment