あなたが持っている配列宣言は、意図したものではありません。intへのポインターへのポインターの2要素配列があります。このページは、これらの宣言を読むための優れたガイドです。
個人的には、typedef を使用して、次のような複雑な型をゼロから構築することを好みます。
typedef int[2] element_type; // this is the 2-element array of ints
typedef element_type* inner_type; // this is the array of unknown size
typedef inner_type[5] outer_type; // this is the actual type we want to use
outer_type output_vertex; // we now have an array of 5 inner_type variables on the stack
// The output_vertex is *uninitialized* so we have to initialize each of its elements
for (int i=0; i < 5; ++i) {
output_vertex[i] = new inner_type[SOME_SIZE];
}
// do stuff with output_vertex now that it's initialized
// then, to prevent memory leaks, delete the memory you allocated
for (int i=0; i < 5; ++i) {
delete[] output_vertex[i];
}
単純化する方法はおそらくありますが、それが出発点です。
inner_type
を追加可能にしたい場合は、生の配列の代わりに使用することを強くお勧めします。std::vector
未加工の配列を使用して行う簿記は非常に多いため、その例は示しません。ただし、多かれ少なかれ、次のようにしますstd::vector
。
typedef std::pair<int,int> element_type; // this is the 2-element array of ints as a pair
typedef std::vector<element_type> inner_type; // dynamic vector this time
inner_type output_vertex[5]; // we now have an array of 5 inner_type variables on the stack
// do stuff with output_vertex
std::vector
動的に割り当てられた配列と同じくらい高速ですが、自分で簿記を行う必要はありません。また、ヒープ割り当てオブジェクトをそれほど多く管理する必要がないという利点もあります。
std::vector
生の配列はコンテナー (例: )と互換性がないことに注意してください。そのため、std::pair
代わりにここで使用します。
C++11 (またはブースト) を使用でき、標準コンテナーに収まる 3 つ以上の項目の固定サイズの配列が必要な場合は、std::array
.