Deprecated: Array and string offset access syntax with curly braces is deprecated in /mnt/hep_web/hep_web/member/n-kota/dokuwiki/inc/init.php on line 542
ja:cpp:array [GiriWiki]

ユーザ用ツール

サイト用ツール


ja:cpp:array

以前のリビジョンの文書です



Warning: "continue" targeting switch is equivalent to "break". Did you mean to use "continue 2"? in /mnt/hep_web/hep_web/member/n-kota/dokuwiki/inc/parser/handler.php on line 1458

Warning: Declaration of syntax_plugin_note::handle($match, $state, $pos, &$handler) should be compatible with DokuWiki_Syntax_Plugin::handle($match, $state, $pos, Doku_Handler $handler) in /mnt/hep_web/hep_web/member/n-kota/dokuwiki/lib/plugins/note/syntax.php on line 79

Warning: Declaration of syntax_plugin_note::render($mode, &$renderer, $indata) should be compatible with DokuWiki_Syntax_Plugin::render($format, Doku_Renderer $renderer, $data) in /mnt/hep_web/hep_web/member/n-kota/dokuwiki/lib/plugins/note/syntax.php on line 101

Warning: preg_match(): Compilation failed: invalid range in character class at offset 3120 in /mnt/hep_web/hep_web/member/n-kota/dokuwiki/inc/parser/lexer.php on line 118
A PCRE internal error occured. This might be caused by a faulty plugin

====== 配列 ====== ---- ===== 動的メモリ確保・解放 ===== === 1次元配列 === <code cpp> ////////// 確保 ////////// double *array1; int n1 = 10; array1 = new double[n1]; ////////// 解放 ////////// delete[] array1; </code> ---- === 2次元配列 === == 方法1 == * 一度に確保できるのは1次元までなので、1次元ずつ確保していく。 * 解放も同様に1次元ずつ。 * 配列ポインタへの配列、という扱いで、最初の足ごとに配列の要素数を変えることも可能 <code cpp> ////////// 確保 ////////// double **array2; int n1 = 10; int n2 = 20; array2 = new double*[n1]; for(int i = 0 ; i < n1 ; ++i){ array2[i] = new double[n2]; } ////////// 解放 ////////// for(int i = 0 ; i < n1 ; ++i){ delete[] array2[i]; } delete[] array2; </code> == 方法2 == 後ろの足のサイズが先に決まっている場合、最初の足だけ動的に確保してやるやり方もできる。 <code cpp> ////////// 確保 ////////// double (*array2)[20]; int n1 = 10; array2 = new double[n1][20]; ////////// 解放 ////////// delete[] array2; </code> == 方法3 == std::vectorでやっちゃう <code cpp> int n1 = 10; int n2 = 20; std::vector< std::vector<double> > array2( n1, std::vector<double>(n2) ); </code> ---- === 3次元配列 === 2次元と同様。 <code cpp> ////////// 確保 ////////// double ***array3; int n1 = 10; int n2 = 20; int n3 = 20; array3 = new double**[n1]; for(int i = 0 ; i < n1 ; ++i){ array3[i] = new double*[n2]; for(int j = 0 ; j < n2 ; ++j){ array3[i][j] = new double[n3]; } } ////////// 解放 ////////// for(int i = 0 ; i < n1 ; ++i){ for(int j = 0 ; j < n2 ; ++j){ delete[] array3[i][j]; } delete[] array3[i]; } delete[] array3; </code> ----

ja/cpp/array.1494749097.txt.gz · 最終更新: 2017/05/14 08:04 by kota