私はC++で独学で学んでいて、それについてもっと学ぶために仕事に限られた余分な時間を費やしているので、私と一緒に耐えてください(私は日中化学工学の研究者です)。
私の目的は非常に単純です。1。フロートの長いリストを格納するためのサイズセーフなコンテナーを作成します。2.マトリックスとして機能するそのコンテナーの特殊バージョンを作成します。
私がここで提起したさまざまな質問に対するいくつかのフィードバックに基づいて、これまでに思いついたのは次のとおりです。
template<typename T>
class VectorDeque
{
public:
void resize_index(unsigned int index) {
if ( my_container == VECTOR ) {
try {
my_vector.resize(index);
my_container = VECTOR;
}
catch(std::bad_alloc &e) {
my_deque.resize(index);
my_container = DEQUE;
}
}
else if ( my_container == DEQUE ) {
my_deque.resize(index);
}
}
T operator[](unsigned int index) {
T ret_val;
if ( STORAGE_CONTAINER == VECTOR ) {
ret_val = my_vector[index];
}
else if ( STORAGE_CONTAINER == DEQUE ) {
ret_val = my_deque[index];
}
}
private:
enum STORAGE_CONTAINER { NONE, DEQUE, VECTOR };
std::vector<T> my_vector;
std::deque<T> my_deque;
STORAGE_CONTAINER my_container;
T& get(int index) {
T temp_val;
if(my_container == VECTOR) {
temp_val = my_vector[index];
}
else if(my_container == DEQUE) {
temp_val = my_deque[index];
}
return temp_val;
}
};
template<typename T>
class VectorDeque2D: public VectorDeque<T>
{
public:
template<typename T>
class VectorDeque2D_Inner_Set
{
VectorDeque2D& parent;
int first_index;
public:
// Just init the temp object
VectorDeque2D_Inner_Set(My2D& p, int first_Index) :
parent(p),
first_Index(first_index) {}
// Here we get the value.
T& operator[](int second_index) const
{ return parent.get(first_index,second_index);}
};
// Return an object that defines its own operator[] that will access the data.
// The temp object is very trivial and just allows access to the data via
// operator[]
VectorDeque2D_Inner_Set<T> operator[](unsigned int first_index) {
return VectorDeque2D_Inner_Set<T>(*this, first_index);
}
void resize_index_second(unsigned int second_index) {
if ( my_container == VECTOR ) {
try {
for (unsigned int couter=0;couter < my_vector.size(); counter++) {
my_vector[counter].resize(second_index);
}
my_container = VECTOR;
}
catch(std::bad_alloc &e) {
for (unsigned int couter=0;couter < my_deque.size(); counter++) {
my_deque[counter].resize(second_index);
}
my_container = DEQUE;
}
}
else if ( my_container == DEQUE ) {
for (unsigned int couter=0;couter < my_deque.size(); counter++) {
my_deque[counter].resize(second_index);
}
}
}
void resize(unsigned int first_index,
unsigned int second_index) {
if ( my_container == VECTOR ) {
try {
my_vector.resize(first_index);
for (unsigned int couter=0;couter < my_vector.size(); counter++) {
my_vector[counter].resize(second_index);
}
my_container = VECTOR;
}
catch(std::bad_alloc &e) {
my_deque.resize(first_index);
for (unsigned int couter=0;couter < my_deque.size(); counter++) {
my_deque[counter].resize(second_index);
}
my_container = DEQUE;
}
}
else if ( my_container == DEQUE ) {
my_deque.resize(first_index);
for (unsigned int couter=0;couter < my_deque.size(); counter++) {
my_deque[counter].resize(second_index);
}
}
}
private:
enum STORAGE_CONTAINER { NONE, DEQUE, VECTOR };
friend class VectorDeque2D_Inner_Set;
std::vector<std::vector<T> > my_vector;
std::deque<std::deque<T> > my_deque;
STORAGE_CONTAINER my_container;
T& get(int first_index,int second_index) {
T temp_val;
if(my_container == VECTOR) {
temp_val = my_vector[first_index][second_index];
}
else if(my_container == DEQUE) {
temp_val = my_deque[first_index][second_index];
}
return temp_val;
}
};
この実装では、次のことを試みました
。1.ラッパーのユーザーにアクセス用の2つのオプション(".get(x、y)"と "[x] [y]")を提示します
。2。ベースのラップされたクラスを持つことで再利用を最大化します。そしてそれを継承してマトリックスを作成します。
3.連続メモリ制限に達した場合に、ベクトルから両端キューに遷移する問題を解決します。
これはまともな解決策のように見えますか?提案?