Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- using namespace std;
- class Tst {
- int i;
- double d;
- static int n;
- public :
- Tst() {
- i = ++n;
- d = 10*rand()/RAND_MAX;
- }
- Tst( int i, double d ):i(i),d(d){}
- bool operator<(const Tst& b)const {
- return d < b.d;
- }
- bool operator==(const Tst& b)const {
- return d == b.d;
- }
- ostream& ins(ostream& os) {
- os<< i << " " << d << endl;
- return os;
- }
- };
- int Tst::n = 0;
- ostream& operator<<(ostream& os, Tst& x) {
- return x.ins( os );
- }
- template <class T> class DArray {
- unsigned sz, cap;
- T *r;
- public:
- class IndexError {
- int r, i;
- public:
- IndexError(int r, int i):r(r), i(i) {}
- ostream& rep(ostream& os) {
- os<<"Index= "<< i <<"should be >=0 and <"<< r << endl;
- return os;
- }
- };
- class iterator;
- friend class iterator;
- class iterator {
- unsigned index;
- DArray &p;
- public:
- iterator(DArray& p, unsigned index =0):p(p),index(index) {}
- T& operator*(){ return p.r[index]; }
- iterator& operator++() {
- if(index<p.sz) index++;
- return *this;
- }
- iterator& operator++(int) {
- iterator x(p, index);
- if(index<p.sz) index++;
- return x;
- }
- bool operator==(const iterator& q) {
- return index == q.index;
- }
- bool operator!=(const iterator& q) {
- return index != q.index;
- }
- };
- iterator begin(){ return iterator(*this); }
- iterator end(){ return iterator(*this, sz); }
- DArray(unsigned n=1):cap(n),sz(n) {
- r = new T[n];
- }
- DArray(const DArray& a) {
- cap = a.cap; sz = a.sz;
- r = new T[sz];
- for(unsigned i; i<sz; i++) r[i] = a.r[i];
- }
- DArray& operator=(const DArray& right) {
- if(this!=&right) {
- delete [] r;
- cap = a.cap; sz = a.sz;
- r = new T[sz];
- for(unsigned i; i<sz; i++) r[i] = a.r[i];
- }
- return *this;
- }
- virtual ~DArray() { delete [] r; }
- T& operator[](int index) {
- if( index < 0 || index >= sz ) {
- throw IndexError( sz, index );
- }
- return r[index];
- }
- void ensureCapacity( unsigned newCap ) {
- if( newCap > cap ) {
- T *b = new T[newCap];
- for(unsigned i; i<sz; i++) b[i] = r[i];
- delete []r;
- r = b;
- cap = newCap;
- }
- }
- unsigned size() { return sz; }
- void add( T& e ) {
- if(sz == cap)
- ensureCapacity( 2+(unsigned)(1.1*cap));
- r[sz++] = e;
- }
- void removeFromPos( int pos ) {
- if( pos>=0 && pos<sz ) {
- for( unsigned i=pos-1; i<sz; i++ ) r[i-1] = r[i];
- sz--;
- }
- }
- int indexOf( T& e) {
- for(unsigned i; i<sz; i++)
- if( e == r.[i]) return i;
- return -1;
- }
- void insertAt( T& e, unsigned pos ) {
- if(sz == cap)
- ensureCapacity(2 +(unsigned)(1.1*cap));
- for(unsigned i=sz-1; i>=pos; i--) r[i+1] = r[i];
- r[pos] = e;
- sz++;
- }
- void forEach( void(*f)(T &)) {
- for(unsigned i=0; i<sz; i++) f(r[i]);
- }
- };
- template< class T> void print( T& x) {
- cout << x << endl;
- }
- int main() {
- int n;
- cin>> n;
- DArray<Tst> a(n);
- a.forEach( print<Tst>);
- cout << endl;
- DArray<Tst>::iterator p=a.begin();
- for(; p!=a.end(); p++ ) {
- cout << *p << endl;
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment