東方算程譚

Oriental Code Talk ── επιστημηが与太をこく、弾幕とは無縁のシロモノ。

目次

Blog 利用状況

ニュース

著作とお薦めの品々は

著作とお薦めの品々は
東方熱帯林へ。

あわせて読みたい

わんくま

  1. 東京勉強会#2
    C++/CLI カクテル・レシピ
  2. 東京勉強会#3
    template vs. generics
  3. 大阪勉強会#6
    C++むかしばなし
  4. 東京勉強会#7
    C++むかしばなし
  5. 東京勉強会#8
    STL/CLRによるGeneric Programming
  6. TechEd 2007 @YOKOHAMA
    C++・C++/CLI・C# 適材適所
  7. 東京勉強会#14
    Making of BOF
  8. 東京勉強会#15
    状態遷移
  9. 名古屋勉強会#2
    WinUnit - お気楽お手軽UnitTest

CodeZine

  1. Cで実現する「ぷちオブジェクト指向」
  2. CUnitによるテスト駆動開発
  3. SQLiteで組み込みDB体験(2007年版)
  4. C++/CLIによるCライブラリの.NET化
  5. C# 1.1からC# 3.0まで~言語仕様の進化
  6. BoostでC++0xのライブラリ「TR1」を先取りしよう (1)
  7. BoostでC++0xのライブラリ「TR1」を先取りしよう (2)
  8. BoostでC++0xのライブラリ「TR1」を先取りしよう (3)
  9. BoostでC++0xのライブラリ「TR1」を先取りしよう (4)
  10. BoostでC++0xのライブラリ「TR1」を先取りしよう (5)
  11. C/C++に対応した、もうひとつのUnitTestFramework ─ WinUnit
  12. SQLiteで"おこづかいちょう"
  13. STL/CLRツアーガイド
  14. マージ・ソート : 巨大データのソート法
  15. ヒープソートのアルゴリズム
  16. C++0xの新機能「ラムダ式」を次期Visual Studioでいち早く試す
  17. .NETでマンデルブロ集合を描く
  18. .NETでマンデルブロ集合を描く(後日談)
  19. C++/CLI : とある文字列の相互変換(コンバージョン)
  20. インテルTBBによる選択ソートの高速化
  21. インテルTBB3.0 によるパイプライン処理
  22. Visual C++ 2010に追加されたSTLアルゴリズム
  23. Visual C++ 2010に追加されたSTLコンテナ「forward_list」
  24. shared_ptrによるObserverパターンの実装
  25. .NETでマンデルブロ集合を描く(番外編) ── OpenCLで超並列コンピューティング
  26. StateパターンでCSVを読む
  27. 状態遷移表からStateパターンを自動生成する
  28. 「ソートも、サーチも、あるんだよ」~標準C++ライブラリにみるアルゴリズムの面白さ
  29. インテルTBBの同期メカニズム
  30. なぜsetを使っちゃいけないの?
  31. WPFアプリケーションで腕試し ~C++でもWPFアプリを
  32. C++11 : スレッド・ライブラリひとめぐり
  33. Google製のC++ Unit Test Framework「Google Test」を使ってみる
  34. メールでデータベースを更新するココロミ
  35. Visitorパターンで遊んでみたよ
  36. Collection 2題:「WPFにバインドできる辞書」と「重複を許す検索set」
  37. Visual C++ 2012:stateless-lambdaとSQLiteのぷち拡張
  38. 「Visual C++ Compiler November 2012 CTP」で追加された6つの新機能

@IT

  1. Vista時代のVisual C++の流儀(前編)Vista到来。既存C/C++資産の.NET化を始めよう!
  2. Vista時代のVisual C++の流儀(中編)MFCから.NETへの実践的移行計画
  3. Vista時代のVisual C++の流儀(後編) STL/CLRによるDocument/Viewアーキテクチャ
  4. C++開発者のための単体テスト入門 第1回 C++開発者の皆さん。テスト、ちゃんとしていますか?
  5. C++開発者のための単体テスト入門 第2回 C++アプリケーションの効率的なテスト手法(CppUnit編)
  6. C++開発者のための単体テスト入門 第3回 C++アプリケーションの効率的なテスト手法(NUnit編)

AWARDS


Microsoft MVP
for Visual Developer - Visual C++


Wankuma MVP
for いぢわる C++


Nyantora MVP
for こくまろ中国茶

Xbox

Links

記事カテゴリ

書庫

日記カテゴリ

多次元配列のforeach

わんくま同盟 東京勉強会 #8 の ぢゃま いかを さんのセッションで
僕が投げた質問:

int [M,N] みたいな多次元配列を foreach したらどないなんの?

わざわざ訊くほどのことでもなかったわけで、復習がてらのお試しコード

class Program {
  public static void Main() {

    int[,,] arr = new int[3,4,5];
    for ( int x = 0; x < 3; ++x ) {
      for ( int y = 0; y < 4; ++y ) {
        for ( int z = 0; z < 5; ++z ) {
          arr[x,y,z] = 9000+ x*100+y*10+z;
        }
      }
    }
    foreach ( int item in arr ) {
      System.Console.Write("{0} ", item);
    }
  }
}

ふーん、[0,0,0] [0,0,1] ... [0,0,4], [0,1,0] [0,1,1] ...
うしろの添字から順にincrementされた順で列挙されます。
# Lengthプロパティは要素の総数が返ってきますねー

ジャグ配列: int[M][N] arr を foreach する場合、
foreach ( int item in arr ) ってやるとコンパイルエラーです。
foreach ( int[] row in arr ) が正解。

[追記] ついでにジャグ配列の列挙順を調べゆ。
class Program {
  static void Main() {
    int[][] arr;
    arr = new int[2][];
    arr[0] = new int[3];
    arr[1] = new int[4];
    System.Console.WriteLine(arr.Length);
    foreach (int[] row in arr) {
      System.Console.WriteLine(row.Length);
    }
  }
}

2, 3, 4 の順でした。左から右/上から下ですねー

投稿日時 : 2007年6月19日 13:56

コメントを追加

# re: 多次元配列のforeach 2007/06/19 17:39 シャノン

> foreach ( int[] row in arr ) が正解。

このループは何回実行されるんですかネ?
M回? N回?

「やってみれば」? ごもっとも。

# re: 多次元配列のforeach 2007/06/19 17:55 片桐

うーん、M回まわってN回……の予感。後ろ解決ってことは、確保したメモリ空間の末尾、メモリアドレスの尻尾というかから順番になめてるって事だから、C的な動きとして考えるなら、ポインタに初期値で末尾を入れて、確保アドレスのスタート位置まで減算してるってことで良いのかなぁなんて考えてみたり。始めに大きさ宣言してインスタンス作ってるからできるですよね、きっと。

# re: 多次元配列のforeach 2007/06/20 1:12 RUN

そもそも、ジャグ配列ってどういう存在のものなん?
メモリーMAPで考えると

って事で、単純にN+M回 回すんじゃないか?
(M+N回じゃないよ、N+M回なんだきっと)

と、ジャグ配列についてよくわかってない人が言ってみる

# re: 多次元配列のforeach 2007/06/20 7:54 ゆーち

センセイ!質問です。

初期値をセットしているループは、foreach できないんですか?

# re: 多次元配列のforeach 2007/06/20 9:49 επιστημη

ゆーちくん、いぃ質問だね。

int[,,] arr = new int[3,4,5];
foreach ( int item in arr ) {
 item = なにやら;
}

ダメなのねー。foreach での loop変数は右辺値なのね。
んだから代入できまひぇん。コンパイル・エラーっす。

# re: 多次元配列のforeach 2007/06/20 21:31 刈歩 菜良

> そもそも、ジャグ配列ってどういう存在のものなん?
> メモリーMAPで考えると
セッションでやったよぉー。
ビデオチェックプリーズです。

そのうち時間ができたらBlog化しますです。

# re: 多次元配列のforeach 2007/06/20 21:36 刈歩 菜良

> 僕が投げた質問:
これって、確か当日お答えした記憶があるのですが、お答えできなくって「後日確認します。」ってどんなご質問でしたっけ?

ビデオチェックしてるんですが、最後の方にあるかともったらなかったのです。
全部チェックする暇が...
(T_T)

# re: 多次元配列のforeach 2007/06/20 21:41 επιστημη

コレじゃなかったっけー?
もいっこ覚えてんのは:

 int[][] arr;
 arr = new int[2][]; // なんで new int[][2] ぢゃないの?

# re: 多次元配列のforeach 2007/06/21 9:10 刈歩 菜良

あれ?
それは「仕様」で逃げたような。(^Q^)

ジャグ配列+foreachでしたっけ?
そんな気がしてきました。

すんません。
土日にビデオチェックしまーす。
<(_ _)>

# re: 多次元配列のforeach 2007/06/21 21:03 RUN

なんと、セッションビデオでジャグ配列まで話をしてたですか。
それは失礼。
早速、ビデオの方見に行ってきます

# Very descriptive article, I loved that bit. Will there be a part 2? 2019/04/07 10:56 Very descriptive article, I loved that bit. Will t

Very descriptive article, I loved that bit. Will there be a part
2?

# Thanks for the auspicious writeup. It if truth be told was once a entertainment account it. Glance complex to far added agreeable from you! By the way, how can we communicate? 2019/04/09 13:47 Thanks for the auspicious writeup. It if truth be

Thanks for the auspicious writeup. It if truth be told was once a
entertainment account it. Glance complex to far added agreeable from you!
By the way, how can we communicate?

# What a information of un-ambiguity and preserveness of precious familiarity on the topic of unpredicted emotions. 2019/07/30 17:38 What a information of un-ambiguity and preservenes

What a information of un-ambiguity and preserveness of precious familiarity
on the topic of unpredicted emotions.

# What a information of un-ambiguity and preserveness of precious familiarity on the topic of unpredicted emotions. 2019/07/30 17:39 What a information of un-ambiguity and preservenes

What a information of un-ambiguity and preserveness of precious familiarity
on the topic of unpredicted emotions.

# What a information of un-ambiguity and preserveness of precious familiarity on the topic of unpredicted emotions. 2019/07/30 17:40 What a information of un-ambiguity and preservenes

What a information of un-ambiguity and preserveness of precious familiarity
on the topic of unpredicted emotions.

# What a information of un-ambiguity and preserveness of precious familiarity on the topic of unpredicted emotions. 2019/07/30 17:41 What a information of un-ambiguity and preservenes

What a information of un-ambiguity and preserveness of precious familiarity
on the topic of unpredicted emotions.

# Hi to every body, it's my first go to see of this web site; this web site carries remarkable and actually fine material designed for visitors. 2019/08/18 23:27 Hi to every body, it's my first go to see of this

Hi to every body, it's my first go to see of this web site; this web site carries remarkable and
actually fine material designed for visitors.

# Hi to every body, it's my first go to see of this web site; this web site carries remarkable and actually fine material designed for visitors. 2019/08/18 23:28 Hi to every body, it's my first go to see of this

Hi to every body, it's my first go to see of this web site; this web site carries remarkable and
actually fine material designed for visitors.

# Hi to every body, it's my first go to see of this web site; this web site carries remarkable and actually fine material designed for visitors. 2019/08/18 23:29 Hi to every body, it's my first go to see of this

Hi to every body, it's my first go to see of this web site; this web site carries remarkable and
actually fine material designed for visitors.

# Hi to every body, it's my first go to see of this web site; this web site carries remarkable and actually fine material designed for visitors. 2019/08/18 23:30 Hi to every body, it's my first go to see of this

Hi to every body, it's my first go to see of this web site; this web site carries remarkable and
actually fine material designed for visitors.

# Hey there! Do you know if they make any plugins to protect against hackers? I'm kinda paranoid about losing everything I've worked hard on. Any tips? 2019/09/03 22:38 Hey there! Do you know if they make any plugins to

Hey there! Do you know if they make any plugins to protect against hackers?
I'm kinda paranoid about losing everything I've worked hard on. Any tips?

# Hey there! Do you know if they make any plugins to protect against hackers? I'm kinda paranoid about losing everything I've worked hard on. Any tips? 2019/09/03 22:39 Hey there! Do you know if they make any plugins to

Hey there! Do you know if they make any plugins to protect against hackers?
I'm kinda paranoid about losing everything I've worked hard on. Any tips?

# Hey there! Do you know if they make any plugins to protect against hackers? I'm kinda paranoid about losing everything I've worked hard on. Any tips? 2019/09/03 22:40 Hey there! Do you know if they make any plugins to

Hey there! Do you know if they make any plugins to protect against hackers?
I'm kinda paranoid about losing everything I've worked hard on. Any tips?

# Hey there! Do you know if they make any plugins to protect against hackers? I'm kinda paranoid about losing everything I've worked hard on. Any tips? 2019/09/03 22:41 Hey there! Do you know if they make any plugins to

Hey there! Do you know if they make any plugins to protect against hackers?
I'm kinda paranoid about losing everything I've worked hard on. Any tips?

# What a material of un-ambiguity and preserveness of precious familiarity regarding unpredicted emotions. 2019/09/06 17:44 What a material of un-ambiguity and preserveness o

What a material of un-ambiguity and preserveness of
precious familiarity regarding unpredicted emotions.

タイトル
名前
URL
コメント