この文書の現在のバージョンと選択したバージョンの差分を表示します。
cli:for_each [2009/01/31 14:57] 192.168.0.63 作成 |
cli:for_each [2013/12/23 09:08] |
||
---|---|---|---|
ライン 1: | ライン 1: | ||
- | ====== for each ====== | ||
- | |||
- | for each構文は配列以外にも様々な型に対して動作します。 | ||
- | |||
- | * IEnumerableインターフェースを実装するクラス。 | ||
- | * STLのiteratorのようなものをもっているクラス。 | ||
- | |||
- | <code cpp> | ||
- | #include <vector> | ||
- | #include <map> | ||
- | using namespace std; | ||
- | using namespace System; | ||
- | using namespace System::Collections; | ||
- | int main(array<System::String ^> ^args) | ||
- | { | ||
- | ArrayList^ al = gcnew ArrayList(); | ||
- | al->Add(1); | ||
- | al->Add(2); | ||
- | |||
- | for each(int a in al) | ||
- | { | ||
- | Console::WriteLine(a); | ||
- | } | ||
- | |||
- | Hashtable^ ht = gcnew Hashtable(); | ||
- | ht["aaa"] = "111"; | ||
- | ht["bbb"] = "222"; | ||
- | for each(DictionaryEntry^ dic in ht) | ||
- | { | ||
- | Console::WriteLine(dic->Key->ToString() + dic->Value->ToString()); | ||
- | } | ||
- | |||
- | vector<int> vi; | ||
- | vi.push_back(3); | ||
- | vi.push_back(4); | ||
- | for each(int i in vi) | ||
- | { | ||
- | Console::WriteLine(i); | ||
- | } | ||
- | |||
- | map<const char*, int> num; | ||
- | |||
- | num["ten"] = 10; | ||
- | num["hundred"] = 100; | ||
- | |||
- | for each( pair<const char*, int> c in num ) | ||
- | { | ||
- | Console::WriteLine(gcnew String(c.first) + c.second.ToString()); | ||
- | } | ||
- | | ||
- | return 0; | ||
- | } | ||
- | </code> |