Guest User

Untitled

a guest
Jun 25th, 2018
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.27 KB | None | 0 0
  1. // typedefs to make our lives easier :D
  2. typedef std::map<unmanaged_cli_ptr<System::String>*, unmanaged_cli_ptr<System::String>*> MyMapType;
  3. typedef std::pair<unmanaged_cli_ptr<System::String>*, unmanaged_cli_ptr<System::String>*> MyMapPairType;
  4.  
  5.  
  6. // finds a value corresponding to key from std::map
  7. unmanaged_cli_ptr<System::String>& FindString(const MyMapType& map, unmanaged_cli_ptr<System::String>* key)
  8. {
  9. for (MyMapType::const_iterator begin = map.begin() ; begin != map.end() ; begin++)
  10. {
  11. if (System::String::Compare(**begin->first, **key) == 0)
  12. {
  13. return *begin->second;
  14. }
  15. }
  16. throw std::exception("cant find key");
  17. }
  18.  
  19. int _tmain(int argc, _TCHAR* argv[])
  20. {
  21. // create a map and insert one item
  22. MyMapType myMapOfStrings;
  23. // insert
  24. System::String^ myEntryKey = "this is a key of map item";
  25. System::String^ myEntryValue = "this is value corresponding to key";
  26. myMapOfStrings.insert(MyMapPairType(new unmanaged_cli_ptr<System::String>(myEntryKey),
  27. new unmanaged_cli_ptr<System::String>(myEntryValue)));
  28.  
  29.  
  30. // find that item and print the value
  31. System::String^ searchForKey = "this is a key of map item";
  32. System::Console::WriteLine(*FindString(myMapOfStrings, &unmanaged_cli_ptr<System::String>(searchForKey)));
  33.  
  34.  
  35. std::cin.get();
  36. return 0;
  37. }
Add Comment
Please, Sign In to add comment