C++CLIでのDictionaryの使い方 一般的なものまとめ
System::Collections::Generic::Dictionary<String^,int>^ dic = gcnew System::Collections::Generic::Dictionary<String^,int>();
dic->Add("零",0); dic->Add("一",1); dic->Add("二",2); dic->Add("三",3); dic->Add("四",4);
int ret; ret = dic["零"]; Console::Write( String::Format("零 : {0}\n",ret) ); //または dic->TryGetValue("一",ret); Console::Write( String::Format("一 : {0}\n",ret) );
if (dic->Remove("二") == true) { Console::WriteLine("成功"); } else { Console::WriteLine("失敗"); } if (dic->Remove("八") == true) { Console::WriteLine("成功"); } else { Console::WriteLine("失敗"); }
System::Collections::Generic::Dictionary<String^, int>^ dup = gcnew System::Collections::Generic::Dictionary<String^, int>(dic);
if (dic->ContainsKey("一")==true) { Console::WriteLine( dic["一"] ); } if (dic->ContainsValue(1) == true) { Console::WriteLine("1 は存在する"); }
for each( String^ key in dic->Keys ) { Console::Write( String::Format("{0}\n",key) ); }
for each( int value in dic->Values ) { Console::Write( String::Format("{0}\n",value) ); }
for each( System::Collections::Generic::KeyValuePair<String^,int> pair in dic) { Console::Write( String::Format("{0} {1}\n",pair.Key,pair.Value) ); }