본문 바로가기
카테고리 없음

벡터_구현

by ByteBridge 2013. 3. 10.
반응형

출처: http://stackoverflow.com/questions/5159061/implementation-of-vector-in-c

//헤더

#pragma once

template <typename T>

class MyVector

{

public:


typedef T * iterator;


MyVector();

MyVector(unsigned int size);

MyVector(unsigned int size, const T & initial);

MyVector(const MyVector<T> & v);      

~MyVector();


unsigned int capacity() const;

unsigned int size() const;

bool empty() const;

iterator begin();

iterator end();

T & front();

T & back();

void push_back(const T & value); 

void pop_back();  


void reserve(unsigned int capacity);   

void resize(unsigned int size);   


T & operator[](unsigned int index);  

MyVector<T> & operator=(const MyVector<T> &);

void clear();

private:

unsigned int my_size;

unsigned int my_capacity;

T * buffer;

};

template<class T>

MyVector<T>::MyVector()

{

my_capacity = 0;

my_size = 0;

buffer = 0;

}


template<typename T>

MyVector<T>::MyVector(const MyVector<T> & v)

{

my_size = v.my_size;

my_capacity = v.my_capacity;

buffer = new T[my_size];  

for (unsigned int i = 0; i < my_size; i++)

buffer[i] = v.buffer[i];  

}


template<typename T>

MyVector<T>::MyVector(unsigned int size)

{

my_capacity = size;

my_size = size;

buffer = new T[size];

}


template<typename T>

MyVector<T>::MyVector(unsigned int size, const T & initial)

{

my_size = size;

my_capacity = size;

buffer = new T [size];

for (unsigned int i = 0; i < size; i++)

buffer[i] = initial;

//T();

}


template<typename T>

MyVector<T> & MyVector<T>::operator = (const MyVector<T> & v)

{

delete[ ] buffer;

my_size = v.my_size;

my_capacity = v.my_capacity;

buffer = new T [my_size];

for (unsigned int i = 0; i < my_size; i++)

buffer[i] = v.buffer[i];

return *this;

}


template<typename T>

typename MyVector<T>::iterator MyVector<T>::begin()

{

return buffer;

}


template<typename T>

typename MyVector<T>::iterator MyVector<T>::end()

{

return buffer + size();

}


template<typename T>

T& MyVector<T>::front()

{

return buffer[0];

}


template<typename T>

T& MyVector<T>::back()

{

return buffer[size - 1];

}


template<typename T>

void MyVector<T>::push_back(const T & v)

{

if (my_size >= my_capacity)

reserve(my_capacity +5);

buffer [my_size++] = v;

}


template<typename T>

void MyVector<T>::pop_back()

{

my_size--;

}


template<typename T>

void MyVector<T>::reserve(unsigned int capacity)

{

if(buffer == 0)

{

my_size = 0;

my_capacity = 0;

}    

T * Newbuffer = new T [capacity];

//assert(Newbuffer);

unsigned int l_Size = capacity < my_size ? capacity : my_size;

//copy (buffer, buffer + l_Size, Newbuffer);


for (unsigned int i = 0; i < l_Size; i++)

Newbuffer[i] = buffer[i];


my_capacity = capacity;

delete[] buffer;

buffer = Newbuffer;

}


template<typename T>

unsigned int MyVector<T>::size()const//

{

return my_size;

}


template<typename T>

void MyVector<T>::resize(unsigned int size)

{

reserve(size);

my_size = size;

}


template<typename T>

T& MyVector<T>::operator[](unsigned int index)

{

return buffer[index];

}  


template<typename T>

unsigned int MyVector<T>::capacity()const

{

return my_capacity;

}


template<typename T>

MyVector<T>::~MyVector()

{

delete[ ] buffer;

}

template <typename T>

void MyVector<T>::clear()

{

my_capacity = 0;

my_size = 0;

buffer = 0;

}


=============================

// 벡터 구현.cpp : 콘솔 응용 프로그램에 대한 진입점을 정의합니다.

//


#include "stdafx.h"

#include "MyVector.h"


int _tmain(int argc, _TCHAR* argv[])

{


MyVector<int> v;


v.reserve(2);

assert(v.capacity() == 2);


MyVector<string> v1(2);

assert(v1.capacity() == 2);

assert(v1.size() == 2);

assert(v1[0] == "");

assert(v1[1] == "");


v1[0] = "hi";

assert(v1[0] == "hi");


MyVector<int> v2(2, 7);

assert(v2[1] == 7);


MyVector<int> v10(v2);

assert(v10[1] == 7);


MyVector<string> v3(2, "hello");

assert(v3.size() == 2);

assert(v3.capacity() == 2);

assert(v3[0] == "hello");

assert(v3[1] == "hello");


v3.resize(1);

assert(v3.size() == 1);

assert(v3[0] == "hello");


MyVector<string> v4 = v3;

assert(v4.size() == 1);

assert(v4[0] == v3[0]);

v3[0] = "test";

assert(v4[0] != v3[0]);  

assert(v4[0] == "hello");


v3.pop_back();

assert(v3.size() == 0);


MyVector<int> v5(7, 9);

MyVector<int>::iterator it = v5.begin();

while (it != v5.end())

{

assert(*it == 9);

++it;

}


MyVector<int> v6;

v6.push_back(100);

assert(v6.size() == 1);

assert(v6[0] == 100);

v6.push_back(101);

assert(v6.size() == 2);

assert(v6[0] == 100);

v6.push_back(101);


cout << "SUCCESS\n";

/*

MyVector<int> v;

v.push_back(23);

v.push_back(2);


for (unsigned int i=0;i<2;i++)

{

cout<<v[i]<<endl;

}

*/

/*

MyVector<string> v;

string ss;

v.push_back("good");

ss = v[0];

cout<<ss<<endl;

*/

return 0;

}



반응형