Шаблонный класс для представления динамических одномерных массивов 


Мы поможем в написании ваших работ!



ЗНАЕТЕ ЛИ ВЫ?

Шаблонный класс для представления динамических одномерных массивов



Шаблонный класс Vect обеспечивает хранение данных любого типа T, для которого предусмотрены конструктор по умолчанию, конструктор копирования и операция присваивания:

// shablon_vector.h

#include "stdafx.h"

#define DEBUG //включено или выключено

#ifndef _VECT_

#define _VECT_

template <class T>

class Vect {

public:

explicit Vect(): first(0), last(0) {}

explicit Vect (size_t _n, const T& _v=T()) {

Allocate(_n);

for (size_t i=0; i<_n; ++i)

*(first +i) = _v;

}

 

Vect(const Vect&);

Vect & operator= (const Vect&);

~Vect(){

#ifdef DEBUG

cout <<" Destructor of " << markName << endl;

#endif

Destroy();

first=0; last=0;

}

 

void mark (std::string& name) {markName = name;}

string mark() const {return markName;}

size_t size() const;

T* begin() const {return first;}

T* end() const {return last;}

T& operator[](size_t i);

void insert (T* _P, const T& _x);

void push_back (const T& _x);

void pop_back();

void show() const;

protected:

void Allocate(size_t _n){

first = new T[_n* sizeof(T)];

last = first + _n;

}

void Destroy(){

for (T* p = first; p!=last; ++p) p->~T();

delete [] first;

}

T* first;

T* last;

string markName;

};

 

 

template<class T>

Vect<T>::Vect(const Vect& other) {

size_t n = other.size();

Allocate(n);

for(size_t i=0; i<n; ++i)

*(first+i)=*(other.first + i);

markName = string("Copy of ") + other.markName;

#ifdef DEBUG

cout << "Copy constructor: " << markName << endl;

#endif

}

 

template<class T>

Vect<T>& Vect<T>::operator=(const Vect& other) {

if (this == &other) return *this;

Destroy();

size_t n = other.size();

Allocate(n);

for(size_t i=0; i<n; ++i)

*(first+i)=*(other.first + i);

return *this;

}

 

template<class T>

size_t Vect<T>::size() const {

if (first > last) {cout << "error " << endl;

_getch();

return -1;

}

return (0==first? 0: last-first);

}

 

template<class T>

T& Vect<T>::operator[](size_t i) {

if (i<0 || i > (size()-1))

{cout << "error " << endl;

_getch();

exit(-1);

}

return (*(first+i));

}

 

template<class T>

void Vect<T>::insert(T* _p, const T& _x) {

size_t n = size()+1;

T* new_first = new T [n*sizeof(T)];

T* new_last = new_first + n;

size_t offset = _p - first;

for (size_t i=0; i<offset; ++i)

*(new_first + i) =*(first + i);

*(new_first + offset) = _x;

for (size_t i=offset; i<n; ++i)

*(new_first + i+1) =*(first+i);

Destroy();

first=new_first;

last=new_last;

}

template<class T>

void Vect<T>::push_back(const T& _x) {

if(!size()) {

Allocate(1);

*first = _x;

}

else insert(end(), _x);

}

template<class T>

void Vect<T>::pop_back() {

if(last==first)

{cout << "error " << endl;

_getch();

exit(-1);

}

T *p=end()-1;

p->~T();

last--;

}

 

template<class T>

void Vect<T>::show() const {

cout << "\n====Contents of " << markName << "=====" << endl;

size_t n = size();

for (size_t i = 0; i< n; ++i)

cout << *(first + i) << " ";

cout << endl;

}

#endif

/////////////////////////////////////////////////////////////////////////////

// stdafx.h:

#pragma once

 

#define WIN32_LEAN_AND_MEAN //Exclude rarely-used stuff from Windows headers

#include <stdio.h>

#include <tchar.h>

#include <iostream>

#include <conio.h>

#include <string>

using namespace std;

#include "shablon_vector.h"

/////////////////////////////////////////////////////////////////////////////

// shablon_vector.cpp

#include "stdafx.h"

template<class T>

void SomeFunction(Vect<T>v);

 

Int main()

{

string initStr[5]= {"first", "second", "third", "fourth", "fifth"};

Vect<int> v1(10);

v1.mark(string("v1"));

size_t n =v1.size();

for (unsigned i=0; i<n; ++i)

v1[i]= i+1;

v1.show();

SomeFunction(v1);

{

Vect<string> v2(5);

v2.mark(string("v2"));

size_t n =v2.size();

for (unsigned i=0; i<n; ++i)

v2[i]= initStr[i];

v2.show();

v2.insert(v2.begin()+3, "After_third");

v2.show();

//cout << v2[6] << endl;//Error!!!

v2.push_back("Add_1");

v2.push_back("Add_2");

v2.push_back("Add_3");

v2.show();

v2.pop_back();

 

v2.pop_back();

v2.show();

}

{

Vect<int> v3;

v3.mark(string("v3"));

v3.push_back(41);

v3.push_back(42);

v3.push_back(43);

v3.show();

 

Vect<int> v4;

v4.mark(string("v4"));

v4=v3;

v4.show();

 

v3.pop_back();v3.pop_back();

v3.pop_back();

v3.show();

}

 

_getch();

return 0;

}

 

template<class T>

void SomeFunction(Vect<T>v){

cout << "Reversive output for " << v.mark() << ":" << endl;

size_t n = v.size();

for (unsigned i = n-1; i>0; --i)

std::cout << v[i] << " ";

cout << endl;

}

 

====Contents of v1=====

1 2 3 4 5 6 7 8 9 10

Reversive output for Copy of v1:

10 9 8 7 6 5 4 3 2

====Contents of v2=====



Поделиться:


Последнее изменение этой страницы: 2016-12-17; просмотров: 217; Нарушение авторского права страницы; Мы поможем в написании вашей работы!

infopedia.su Все материалы представленные на сайте исключительно с целью ознакомления читателями и не преследуют коммерческих целей или нарушение авторских прав. Обратная связь - 18.221.129.19 (0.008 с.)