본문 바로가기
01.Bit 단기/C++

50_배열 템플릿 사용자 정의클래스

by chojju 2018. 5. 9.
반응형

//50_배열 템플릿 사용자 정의클래스.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;
}


 

//50_배열 템플릿 사용자 정의클래스 저장.cpp

#include <iostream>
using namespace std;
#include "50_배열 템플릿 사용자 정의클래스.h"


//50_메인.cpp

#include <iostream>
#include <algorithm> //STL 알고리즘 함수
#include<vector>        //vector 컨테이너 h
using namespace std;
#include <time.h>


//data class
class Member
{
 string name;
 int age;
public:
 Member(string _name, int _age)
  :name(_name), age(_age)
 {
 }
 void Print() const
 {
  cout << name.c_str() << " / " << age << endl;
 }
};

void fun_print()
{
 vector <int*> vec1;
 //현재 저장 개수
 cout << "사이즈 : " << vec1.size() << endl;
 //저장할 수 있는 생성된 공간
 cout << "크기 : " << vec1.capacity() << endl;
 //저장할 수 있는 최대 저장공간
 cout << "max : " << vec1.max_size() << endl;
}


int main()
{
 vector <int*> vec1;

 fun_print();

 vec1.push_back(new int(1));
 vec1.push_back(new int(2));
 vec1.push_back(new int(3));

 return 0;
}

 

반응형

'01.Bit 단기 > C++' 카테고리의 다른 글

52_컨테이너와 find  (0) 2018.05.10
51_vector  (0) 2018.05.09
49_배열 템플릿(overflow, pushback, erase 구현)  (0) 2018.05.09
48_템플릿클래스로 변경  (0) 2018.05.09
47_char 저장 컨테이너  (0) 2018.05.09

댓글