テンプレートについてもっと知りたいのですが、解決できないような問題に遭遇しました。現時点では、以下のクラスは正常に機能しています。
#include <iostream>
#include <vector>
#include <cstring>
using namespace std;
template <class T, int s>
class myArray{
public:
T* data;
inline T& operator[](const int i){return data[i];}
myArray(){
data=new T[s];
}
myArray(const myArray& other){
data=new T[s];
copy(other.data,other.data+s,data);
}
myArray& operator=(const myArray& other){
data=new T[s];
copy(other.data,other.data+s,data);
return *this;
}
~myArray(){delete [] data;}
};
私がそれを使用する場合:
myArray<myArray<myArray<int,10>,20>,30> a;
aは30x20x10の配列になり、通常の配列ブラケット(a [5] [5] [5]など)でアクセスできます。私が書くことができるように機能を追加したいと思います:
myArray<myArray<myArray<int,10>,20>,30> a(10);
たとえば、すべてのエントリを10に初期化します。私はこれを行う方法を理解することはできません。私が理解しているように、myArrayの各レイヤーはデフォルトのコンストラクターを使用して構築されます。これを次のようなものに変更した場合:
myArray(int n=0){
data=new T[s];
fill(data,data+s,n); //T might not be of type int so this could fail.
}
データがint型でない場合(つまり、次元が1より大きい配列の場合)、これは失敗するはずですが、そうではありません。配列が正方形の場合は機能しますが、そうでない場合は、一部のエントリが10に設定されていません。標準のベクトルクラスがこれをどのように実現するかを知っている人はいますか?どんな助けでも素晴らしいでしょう。ありがとう!