データ拡張
1315 ワード
#include <iostream>
#include <vector>
#include <type_traits>
#include <initializer_list>
template<typename T>
class Tablet{
private:
std::vector<T> data;// .
unsigned int tabletSize;// .
unsigned int slots;// .
public:
template<typename U, class=typename std::enable_if< std::is_same<T, U>::value, void >::type>
Tablet(const std::initializer_list<U>& dataIl);
~Tablet();
void insertData(const T& myData);
};
template<typename U, class>
Tablet::Tablet(const initializer_list<U>& dataIl)
:data(dataIl),
tabletSize(0),
slots(0)
{
if(dataIl.empty()){
;
}else{
this->tabletSize=data.capacity();// std::vector<T> .
this->slots=data.size();// std::vector<T> .
}
}
template<typename U>
Tablet::~Tablet()
{
if(data.empty()){
;
}else{
data.clear();
}
}
template<typename U>
void Tablet::insertData()
{
if(this->tabletSize == 0){
this->tabletSize=1;
}
if(this->slots == this->tabletSize){
data.resize(tabletSize * 2);
data.insert(myData);
this->tabletSize=data.capacity();
this->slots=data.size();
}
}