/ Myvector.h: interface for the Myvector class.
//
//////////////////////////////////////////////////////////////////////
//
//////////////////////////////////////////////////////////////////////
#if !defined(AFX_MYVECTOR_H__52DAFC8E_7E37_4A7C_8A3C_C7A802D536CD__INCLUDED_)
#define AFX_MYVECTOR_H__52DAFC8E_7E37_4A7C_8A3C_C7A802D536CD__INCLUDED_
#define AFX_MYVECTOR_H__52DAFC8E_7E37_4A7C_8A3C_C7A802D536CD__INCLUDED_
#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
#include <iostream>
using namespace std;
#pragma once
#endif // _MSC_VER > 1000
#include <iostream>
using namespace std;
template <class T>
class Myvector
{
T *base;
int Capacity;
int Usage;
public:
Myvector(int _capacity=0,T _in=0);
virtual ~Myvector();
T& operator[](int index);
int GetUsage();
int GetCapacity();
void PushBack(T in);
void Erase(int index);
void ExtendStorage();
void view();
bool AvalIndex(int index);
void initialization();
};
class Myvector
{
T *base;
int Capacity;
int Usage;
public:
Myvector(int _capacity=0,T _in=0);
virtual ~Myvector();
T& operator[](int index);
int GetUsage();
int GetCapacity();
void PushBack(T in);
void Erase(int index);
void ExtendStorage();
void view();
bool AvalIndex(int index);
void initialization();
};
template <class T>
void Myvector<T>::initialization()
{
Capacity = 0;
Usage = 0 ;
base = 0;
}
template <class T>
Myvector<T>::Myvector(int _capacity,T _in)
{
initialization();
base = new T[_capacity];
Capacity = _capacity;
int i=0;
for(i=0;i<_capacity;i++)
{
PushBack(_in);
}
}
template <class T>
void Myvector<T>::PushBack(T in)
{
if(!Capacity)
{
T* temp;
temp = base;
Capacity++;
base = new T[Capacity];
delete[] temp;
}
if(Capacity == Usage)
{
ExtendStorage();
}
base[Usage++] = in;
}
template <class T>
Myvector<T>::~Myvector()
{
delete[] base;
}
template <class T>
T& Myvector<T>::operator[](int _index)
{
if(!AvalIndex(_index))
{
throw "T& Myvector<T>::operator[](int _index)에서 잘못된 인덱스 사용";
}
return base[_index];
}
template <class T>
int Myvector<T>::GetUsage()
{
return Usage;
}
template <class T>
int Myvector<T>::GetCapacity()
{
return Capacity;
}
template <class T>
void Myvector<T>::ExtendStorage()
{
Capacity = Capacity*2;
T* temp;
temp = base;
base = new T[Capacity];
int i=0;
for(i=0;i<Usage;i++)
{
base[i] = temp[i];
}
}
template <class T>
void Myvector<T>::Erase(int index)
{
int i=0;
for(i=index;i<=Usage;i++)
{
if(Capacity == Usage)
{
Usage--;
}
else if(Capacity != Usage)
{
base[i] = base[i+1];
Usage--;
}
}
}
template <class T>
void Myvector<T>::view()
{
int i=0;
for(i=0;i<Capacity;i++)
{
if(i<Usage)
cout<<"["<<base[i]<<"]";
if(i>=Usage)
cout<<"[x]";
}
cout<<endl;
}
template <class T>
bool Myvector<T>::AvalIndex(int _index)
{
return _index < Usage;
}
#endif // !defined(AFX_MYVECTOR_H__52DAFC8E_7E37_4A7C_8A3C_C7A802D536CD__INCLUDED_)
void Myvector<T>::initialization()
{
Capacity = 0;
Usage = 0 ;
base = 0;
}
template <class T>
Myvector<T>::Myvector(int _capacity,T _in)
{
initialization();
base = new T[_capacity];
Capacity = _capacity;
int i=0;
for(i=0;i<_capacity;i++)
{
PushBack(_in);
}
}
template <class T>
void Myvector<T>::PushBack(T in)
{
if(!Capacity)
{
T* temp;
temp = base;
Capacity++;
base = new T[Capacity];
delete[] temp;
}
if(Capacity == Usage)
{
ExtendStorage();
}
base[Usage++] = in;
}
template <class T>
Myvector<T>::~Myvector()
{
delete[] base;
}
template <class T>
T& Myvector<T>::operator[](int _index)
{
if(!AvalIndex(_index))
{
throw "T& Myvector<T>::operator[](int _index)에서 잘못된 인덱스 사용";
}
return base[_index];
}
template <class T>
int Myvector<T>::GetUsage()
{
return Usage;
}
template <class T>
int Myvector<T>::GetCapacity()
{
return Capacity;
}
template <class T>
void Myvector<T>::ExtendStorage()
{
Capacity = Capacity*2;
T* temp;
temp = base;
base = new T[Capacity];
int i=0;
for(i=0;i<Usage;i++)
{
base[i] = temp[i];
}
}
template <class T>
void Myvector<T>::Erase(int index)
{
int i=0;
for(i=index;i<=Usage;i++)
{
if(Capacity == Usage)
{
Usage--;
}
else if(Capacity != Usage)
{
base[i] = base[i+1];
Usage--;
}
}
}
template <class T>
void Myvector<T>::view()
{
int i=0;
for(i=0;i<Capacity;i++)
{
if(i<Usage)
cout<<"["<<base[i]<<"]";
if(i>=Usage)
cout<<"[x]";
}
cout<<endl;
}
template <class T>
bool Myvector<T>::AvalIndex(int _index)
{
return _index < Usage;
}
#endif // !defined(AFX_MYVECTOR_H__52DAFC8E_7E37_4A7C_8A3C_C7A802D536CD__INCLUDED_)
// Myvector.cpp: implementation of the Myvector class.
//
//////////////////////////////////////////////////////////////////////
//
//////////////////////////////////////////////////////////////////////
#include "Myvector.h"
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
/*
void ExampleSequence()
{
Myvector<int> c;
c.PushBack(0);
c.PushBack(1);
c.PushBack(2);
c.PushBack(3);
c.PushBack(4);
// c.PushBack(5);
// c.PushBack(6);
// c.PushBack(7);
// c.PushBack(8);
c.Erase(4);
c.view();
}
*/
void ExampleHash()
{
Myvector<int> arr(50);
{
Myvector<int> arr(50);
try
{
arr[3] = 7;
arr[9] = 2;
int re = arr[3];
re = arr[9];
int i = 60;
// arr[i] = 7;
{
arr[3] = 7;
arr[9] = 2;
int re = arr[3];
re = arr[9];
int i = 60;
// arr[i] = 7;
arr.view();
}
catch(char *emsg)
{
cout<<emsg<<endl;
}
}
}
catch(char *emsg)
{
cout<<emsg<<endl;
}
}
void main()
{
ExampleHash();
}
{
ExampleHash();
}
'Sunmoon_BIT > C++ programing' 카테고리의 다른 글
RTTI (0) | 2010.04.15 |
---|---|
AVL 트리 회전 (0) | 2009.12.23 |
MyVector를 사용한 학생관리 - C++ (0) | 2009.11.20 |
TEMPLATE - C++ (0) | 2009.11.17 |
Effective C++ 요약 (0) | 2009.10.26 |