//49_배열 템플릿.h
#pragma once
//void* : 주소타입
//T : 값, 주소타입
template<typename T>
class BitArrayList
{
T * arr; //저장소
int max; //저장 최대값
int size; //저장개수 및 저장할위치
public:
BitArrayList(int max = 10);
~BitArrayList();
public:
int getMax() { return max; }
int getSize() { return size; }
T getData(int idx) { return arr[idx]; }
public:
bool pushBack(T value);
bool Erase(int idx);
private:
bool isOverflow();
};
template<typename T>
BitArrayList<T>::BitArrayList(int max /*=10*/)
{
size = 0;
//this : 자신의 개체 주소를 갖는 변수
this->max = max;
// arr = (int**)malloc(sizeof(int*)*max);
arr = new T[max];
}
template<typename T>
BitArrayList<T>::~BitArrayList()
{
// free(arr);
delete[] arr;
}
template<typename T>
bool BitArrayList<T>::pushBack(T value)
{
if (isOverflow() == true)
return false;
arr[size] = value;
size++;
return true;
}
template<typename T>
bool BitArrayList<T>::isOverflow()
{
if (max <= size) return true;
else return false;
}
template<typename T>
bool BitArrayList<T>::Erase(int idx)
{
if (idx >= 0 && idx < size)
{
delete arr[idx]; //<= 동적메모리삭제
for (int i = idx; i < size - 1; i++)
{
arr[i] = arr[i + 1];
}
size--;
return true;
}
else
return false;
}
//49_배열 템플릿.cpp
#include <iostream>
using namespace std;
#include "49_배열 템플릿.h"
//49_메인
#include <iostream>
#include <algorithm> //STL 알고리즘 함수
using namespace std;
#include <time.h>
#include "49_배열 템플릿.h"
int main()
{
BitArrayList<int> arr;
arr.pushBack(10);
arr.pushBack(20);
cout << arr.getData(1) << endl;
BitArrayList<char*> arr1;
arr1.pushBack(new char('a'));
arr1.pushBack(new char('b'));
cout << *arr1.getData(1) << endl;
return 0;
}
'01.Bit 단기 > C++' 카테고리의 다른 글
51_vector (0) | 2018.05.09 |
---|---|
50_배열 템플릿 사용자 정의클래스 (0) | 2018.05.09 |
48_템플릿클래스로 변경 (0) | 2018.05.09 |
47_char 저장 컨테이너 (0) | 2018.05.09 |
46_int 저장 컨테이너 (0) | 2018.05.09 |
댓글