for each構文は配列以外にも様々な型に対して動作します。
#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; }