26

Cおよび/またはC ++で動的(すべての次元が実行時までわからない)多次元配列を操作するために受け入れられている/最も一般的に使用されている方法は何ですか.

このJavaコードが行うことを達成するための最もクリーンな方法を見つけようとしています:

public static void main(String[] args){
 Scanner sc=new Scanner(System.in);
 int rows=sc.nextInt();
 int cols=sc.nextInt();
 int[][] data=new int[rows][cols];
 manipulate(data);
}

public static void manipulate(int[][] data){
   for(int i=0;i<data.length;i++)
   for(int j=0;j<data[0].length.j++){
         System.out.print(data[i][j]);       
   }    
}

(寸法が実行時まで不明であることを明確にするためだけに std_in から読み取ります)。

編集:この質問はかなり古いものですが、かなり人気があることに気付きました。私は実際にトップ投票の回答に同意しません。C の最適な選択は、Guge が以下で述べたように、1 次元配列を使用することだと思います

C++ にはいくつかの選択肢があります。boost または stl が本当に好きな場合は、以下の回答が望ましいかもしれませんが、最も単純でおそらく最速の選択肢は、C のように 1 次元配列を使用することです。

[][] 構文が必要な場合の C および C++ での別の実行可能な選択肢は、下部にある lillq の答えは、多数の malloc を使用して手動で配列を構築することです。

4

10 に答える 10

21

boost::multi_arrayを使用します。

あなたの例のように、コンパイル時に知っておく必要があるのは次元数だけです。ドキュメントの最初の例は次のとおりです。

#include "boost/multi_array.hpp"
#include <cassert>

int 
main () {
  // Create a 3D array that is 3 x 4 x 2
  typedef boost::multi_array<double, 3> array_type;
  typedef array_type::index index;
  array_type A(boost::extents[3][4][2]);

  // Assign values to the elements
  int values = 0;
  for(index i = 0; i != 3; ++i) 
    for(index j = 0; j != 4; ++j)
      for(index k = 0; k != 2; ++k)
        A[i][j][k] = values++;

  // Verify values
  int verify = 0;
  for(index i = 0; i != 3; ++i) 
    for(index j = 0; j != 4; ++j)
      for(index k = 0; k != 2; ++k)
        assert(A[i][j][k] == verify++);

  return 0;
}

編集:コメントで示唆されているように、実行時に多次元配列のサイズを定義し、コンソール入力から要求できる「単純な」サンプルアプリケーションを次に示します。以下は、このサンプル アプリケーションの出力例です (3 次元であるという定数でコンパイルされています)。

Multi-Array test!
Please enter the size of the dimension 0 : 4

Please enter the size of the dimension 1 : 6

Please enter the size of the dimension 2 : 2

Text matrix with 3 dimensions of size (4,6,2) have been created.

Ready!
Type 'help' for the command list.

>read 0.0.0
Text at (0,0,0) :
  ""

>write 0.0.0 "This is a nice test!"
Text "This is a nice test!" written at position (0,0,0)

>read 0.0.0
Text at (0,0,0) :
  "This is a nice test!"

>write 0,0,1 "What a nice day!"
Text "What a nice day!" written at position (0,0,1)

>read 0.0.0
Text at (0,0,0) :
  "This is a nice test!"

>read 0.0.1
Text at (0,0,1) :
  "What a nice day!"

>write 3,5,1 "This is the last text!"
Text "This is the last text!" written at position (3,5,1)

>read 3,5,1
Text at (3,5,1) :
  "This is the last text!"

>exit

コードの重要な部分は、ユーザーから次元を取得して配列を作成する main 関数です。

const unsigned int DIMENSION_COUNT = 3; // dimension count for this test application, change it at will :)

// here is the type of the multi-dimensional (DIMENSION_COUNT dimensions here) array we want to use
// for this example, it own texts
typedef boost::multi_array< std::string , DIMENSION_COUNT > TextMatrix;

// this provide size/index based position for a TextMatrix entry.
typedef std::tr1::array<TextMatrix::index, DIMENSION_COUNT> Position; // note that it can be a boost::array or a simple array

/*  This function will allow the user to manipulate the created array
    by managing it's commands.
    Returns true if the exit command have been called.
*/
bool process_command( const std::string& entry, TextMatrix& text_matrix );

/* Print the position values in the standard output. */
void display_position( const Position& position );

int main()
{
    std::cout << "Multi-Array test!" << std::endl;

    // get the dimension informations from the user
    Position dimensions; // this array will hold the size of each dimension 

    for( int dimension_idx = 0; dimension_idx < DIMENSION_COUNT; ++dimension_idx )
    {
        std::cout << "Please enter the size of the dimension "<< dimension_idx <<" : ";
        // note that here we should check the type of the entry, but it's a simple example so lets assume we take good numbers
        std::cin >> dimensions[dimension_idx]; 
        std::cout << std::endl;

    }

    // now create the multi-dimensional array with the previously collected informations
    TextMatrix text_matrix( dimensions );

    std::cout << "Text matrix with " << DIMENSION_COUNT << " dimensions of size ";
    display_position( dimensions );
    std::cout << " have been created."<< std::endl;
    std::cout << std::endl;
    std::cout << "Ready!" << std::endl;
    std::cout << "Type 'help' for the command list." << std::endl;
    std::cin.sync();


    // we can now play with it as long as we want
    bool wants_to_exit = false;
    while( !wants_to_exit )
    {
        std::cout << std::endl << ">" ;
        std::tr1::array< char, 256 > entry_buffer; 
        std::cin.getline(entry_buffer.data(), entry_buffer.size());

        const std::string entry( entry_buffer.data() );
        wants_to_exit = process_command( entry, text_matrix );
    }

    return 0;
}

そして、配列内の要素にアクセスするのは非常に簡単であることがわかります。次の関数のように operator() を使用するだけです:

void write_in_text_matrix( TextMatrix& text_matrix, const Position& position, const std::string& text )
{
    text_matrix( position ) = text;
    std::cout << "Text \"" << text << "\" written at position ";
    display_position( position );
    std::cout << std::endl;
}

void read_from_text_matrix( const TextMatrix& text_matrix, const Position& position )
{
    const std::string& text = text_matrix( position );
    std::cout << "Text at ";
    display_position(position);
    std::cout << " : "<< std::endl;
    std::cout << "  \"" << text << "\"" << std::endl;
}

注 : このアプリケーションを VC9 + SP1 でコンパイルしました - 忘れがちな警告がいくつか表示されました。

于 2008-12-13T21:52:51.943 に答える
9

C++ で 2 次元配列を表す方法は 2 つあります。一方は他方よりも柔軟です。

配列の配列

最初にポインターの配列を作成し、次に各ポインターを別の配列で初期化します。

// First dimension
int** array = new int*[3];
for( int i = 0; i < 3; ++i )
{
    // Second dimension
    array[i] = new int[4];
}

// You can then access your array data with
for( int i = 0; i < 3; ++i )
{
    for( int j = 0; j < 4; ++j )
    {
        std::cout << array[i][j];
    }
}

この方法の問題点は、2 番目の次元が多数の配列として割り当てられるため、メモリ アロケータの作業が容易にならないことです。メモリが断片化され、パフォーマンスが低下する可能性があります。ただし、2 番目の次元の各配列のサイズが異なる可能性があるため、柔軟性が向上します。

すべての値を保持する大きな配列

ここでの秘訣は、必要なすべてのデータを保持する大規模な配列を作成することです。難しいのは、array[i][j] 構文を使用してデータにアクセスできるようにしたい場合、ポインタの最初の配列が必要なことです。

int* buffer = new int[3*4];   
int** array = new int*[3];

for( int i = 0; i < 3; ++i )
{
    array[i] = array + i * 4;
}

int* 配列は必須ではありません。値の 2 次元座標からバッファー内のインデックスを計算することで、バッファー内のデータに直接アクセスできるからです。

// You can then access your array data with
for( int i = 0; i < 3; ++i )
{
    for( int j = 0; j < 4; ++j )
    {
        const int index = i * 4 + j;
        std::cout << buffer[index];
    }
}

覚えておきたいルール

コンピューターのメモリは線形であり、長期間維持されます。2 次元配列はコンピューターでネイティブにサポートされていないため、唯一の方法は配列を 1 次元配列に "線形化" することです。

于 2008-12-14T01:59:25.750 に答える
5

cols sizeof(int) を割り当て、table[row*cols+col] でアクセスできます。

于 2008-12-13T21:44:17.623 に答える
4

boost を使用しない標準的な方法は、 std::vector を使用することです。

std::vector< std::vector<int> > v;
v.resize(rows, std::vector<int>(cols, 42)); // init value is 42
v[row][col] = ...;

これにより、必要なメモリが自動的に新規作成/削除されます。しかし、主にそのように使用するように設計されていないため(互いにstd::vectorネストする) 、かなり遅いです。std::vectorたとえば、すべてのメモリが 1 つのブロックに割り当てられるのではなく、列ごとに個別に割り当てられます。また、行はすべて同じ幅である必要はありません。より高速なのは、法線ベクトルを使用してcol_count * row + colから、特定の行と列を取得するようなインデックス計算を行うことです。

std::vector<int> v(col_count * row_count, 42);
v[col_count * row + col) = ...;

ただし、これにより、 を使用してベクトルにインデックスを付ける機能が失われます[x][y]。また、行と列の量をどこかに保存する必要がありますが、ネストされたソリューションを使用すると、を使用して行の量と列の量を取得できv.size()ますv[0].size()

ブーストを使用するboost::multi_arrayと、正確に必要なことを行う を使用できます(他の回答を参照)。


ネイティブ C++ 配列を使用する生の方法もあります。これにはかなりの作業が必要であり、ネストされたベクトル ソリューションより優れているとは言えません。

int ** rows = new int*[row_count];
for(std::size_t i = 0; i < row_count; i++) {
    rows[i] = new int[cols_count];
    std::fill(rows[i], rows[i] + cols_count, 42);
}

// use it... rows[row][col] then free it...

for(std::size_t i = 0; i < row_count; i++) {
    delete[] rows[i];
}

delete[] rows;

ポインターから受け取ることができないため、作成した列と行の量をどこかに保存する必要があります。

于 2008-12-13T22:17:59.303 に答える
3

C および C++ の 2D C スタイル配列は、サイズがrows * columns * sizeof(datatype)バイトのメモリのブロックです。

実際の [行][列] 次元は、コンパイル時に静的にのみ存在します。実行時に動的に存在するものは何もありません!

したがって、他の人が述べたように、実装できます

  int array [ rows ] [ columns ];

として:

 int  array [ rows * columns ]

または次のように:

 int * array = malloc ( rows * columns * sizeof(int) );

次へ: 可変サイズの配列を宣言します。 Cではこれが可能です:

int main( int argc, char ** argv )
{
  assert( argc > 2 );

  int rows    = atoi( argv[1] );
  int columns = atoi( argv[2] );

  assert(rows > 0 && columns > 0);
  int data [ rows ] [ columns ];  // Yes, legal!

  memset( &data, 0, sizeof(data) );

  print( rows, columns, data );
  manipulate( rows, columns, data );
  print( rows, columns, data );
}

C では、可変サイズの配列を非可変サイズの配列と同じように渡すことができます。

void manipulate( int theRows, int theColumns, int theData[theRows][theColumns] )
{
  for (   int r = 0; r < theRows;    r ++ )
    for ( int c = 0; c < theColumns; c ++  )
      theData[r][c] = r*10 + c;
}

ただし、C++ ではそれができません。動的割り当てを使用して配列を割り当てる必要があります。たとえば、次のようになります。

int *array = new int[rows * cols]();

またはできれば(自動メモリ管理を使用)

std::vector<int> array(rows * cols);

次に、1 次元データを受け入れるように関数を変更する必要があります。

void manipulate( int theRows, int theColumns, int *theData )
{
  for (   int r = 0; r < theRows;    r ++ )
    for ( int c = 0; c < theColumns; c ++  )
      theData[r * theColumns + c] = r*10 + c;
}
于 2008-12-14T00:31:58.890 に答える
2

C++ の代わりに C を使用している場合は、Dave Hanson のライブラリC Interfaces and Implementationsの Array_T 抽象化を参照してください。それは非常にきれいで、よく設計されています。演習として、生徒に 2 次元バージョンを作成してもらいます。それを行うか、単にインデックス マッピングを行う追加の関数を記述することができます。

void *Array_get_2d(Array_T a, int width, int height, int i, int j) {
    return Array_get(a, j * width, i, j);
}

幅、高さ、および要素へのポインターを格納する別の構造体を持つ方が少しきれいです。

于 2008-12-13T22:13:39.310 に答える
0

これを達成するために malloc を使用できますが、通常の array[][] 平均、array[rows * cols + cols] メソッドを介してアクセスできます。

main()
{
   int i;
   int rows;
   int cols;
   int **array = NULL;

   array = malloc(sizeof(int*) * rows);
   if (array == NULL)
       return 0;  // check for malloc fail

   for (i = 0; i < rows; i++)
   {
       array[i] = malloc(sizeof(int) * cols)
       if (array[i] == NULL)
           return 0;  // check for malloc fail
   }

   // and now you have a dynamically sized array
}
于 2008-12-14T17:16:40.407 に答える
-1

C++ で特定の配列の長さを決定する方法はありません。最善の方法は、おそらく配列の各次元の長さを渡し、配列自体の .length プロパティの代わりにそれを使用することです。

于 2008-12-13T21:43:04.503 に答える