Cod sursa(job #1097138)

Utilizator Theorytheo .c Theory Data 3 februarie 2014 01:16:31
Problema A+B Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 4.05 kb
#include <iostream>
#include <algorithm>
#include <cassert>

using namespace std;

template <class T>
class Vector {

    private:

    unsigned int my_size;
    unsigned int my_capacity;

    T* buffer;
    public :

    typedef T* iterator;

    Vector();
    Vector(unsigned int _size);
    Vector(unsigned int _size, const T &intitial);
    Vector(const Vector<T> &V);
    ~Vector();

    unsigned capacity() const;
    unsigned size() const;
    bool empty() const;

    iterator begin();
    iterator end();

    void push_back(const T& value);
    void pop_back();

    T& front();
    T& back();

    void reserve(unsigned int capacity);
    void resize(unsigned int size);

    T& operator [](unsigned index);
    Vector<T>& operator =(const Vector<T> &);



};

template<class T>
void Vector<T>::resize(unsigned int size){
    this -> my_size = size;
}

template <class T>
typename Vector<T>::iterator Vector<T>::begin() {

    return buffer;
}

template <class T>
typename Vector<T>::iterator Vector<T>::end() {
    return buffer + this -> my_size;
}

template <class T>
T& Vector<T>::front() {
    return this -> buffer[0];
}

template <class T>
T& Vector<T>::back(){

    return this -> buffer[this -> my_size - 1];
}

template <class T>
Vector<T>::Vector() {

    this -> my_size = 0;
    this -> my_capacity = 0;
    this -> buffer = 0;
}

template <class T>
Vector<T>::~Vector() {
     delete []buffer;
}

template<class T>
Vector<T>::Vector(unsigned int _size){

    this -> my_capacity = _size;
    this -> my_size = _size;
    this -> buffer = new T[_size];
}

template<class T>
Vector<T>::Vector(unsigned int _size, const T& initial) {

    this -> my_capacity = _size;
    this -> my_size = _size;
    this -> buffer = new T[_size];

    for(int i = 0 ; i < this -> my_size; ++i)
        buffer[i] = initial;
}

template <class T>
Vector<T>::Vector(const Vector<T> &C) {

    this -> my_size = C.my_size;
    this -> my_capacity = C.my_capacity;
    this -> buffer = new T[this -> my_size];

    for(int i = 0 ;i < C.my_size; ++i)
        this -> buffer[i] = C.buffer[i];
}

template<class T>
unsigned int Vector<T>::capacity()const {

    return this -> my_capacity;
}

template<class T>
unsigned int Vector<T>::size()const {

    return this -> my_size;
}

template <class T>
bool Vector<T>::empty()const{
    return this -> size == 0;
}

template<class T>
T& Vector<T>::operator[] (unsigned int index){

    return this -> buffer[index];
}

template <class T>
Vector<T>& Vector<T>::operator= (const Vector<T> &V) {

    delete[] this -> buffer;
    this -> my_size = V.my_size;
    this -> my_capacity = V.my_capacity;
    this -> buffer = new T[V.my_capacity];

    for(int i = 0 ; i < V.my_size; ++i)
        this -> buffer[i] = V.buffer[i];
    return *this;

}

template<class T>
void Vector<T>::reserve(unsigned int capacity) {

    if(buffer == 0 ){

        this -> my_size = 0;
        this -> my_capacity = 0;
    }

    T* _buffer = new T[capacity];
    copy(this -> buffer, this -> buffer + this -> my_size, _buffer);
    this -> my_capacity = capacity;
    delete[] this -> buffer;
    this -> buffer = _buffer;

}

template<class T>
void Vector<T>::push_back(const T & value) {

    if(this -> my_capacity <= this -> my_size )
            reserve(this -> my_capacity * 2);
    this -> buffer[this -> my_size++] = value;
}

template<class T>
void Vector<T>::pop_back(){

    this -> my_size--;
}

Vector<double> V;

int main() {

    V.push_back(2.4); V.push_back(4.2); V.push_back(5.6);
    Vector<double> G(V);
    cout << G[1] <<" ";
    cout << V[1] << '\n';

   Vector<string> v1(2);
   assert(v1.capacity() == 2);
   assert(v1.size() == 2);
   assert(v1[0] == "");
   assert(v1[1] == "");


   v1.reserve(10);
   assert(v1.capacity() == 10);

   V.pop_back();
   assert(V[V.size() - 1] == 4.2);
   Vector<double> C = V;
   assert(C[0] == V[0]);


    cout << "SUCCES\n";
    return 0;
}