fork(2) download
  1. #include <map>
  2. #include <iostream>
  3.  
  4. typedef std::map<std::string, std::string> MAP;
  5.  
  6. void function(const MAP &map, const std::string &findMe) {
  7. try {
  8. const std::string& value = map.at(findMe);
  9. std::cout << "Value of key \"" << findMe.c_str() << "\": " << value.c_str() << std::endl;
  10. // TODO: Handle the element found.
  11. }
  12. catch (const std::out_of_range&) {
  13. std::cout << "Key \"" << findMe.c_str() << "\" not found" << std::endl;
  14. // TODO: Deal with the missing element.
  15. }
  16. }
  17.  
  18. int main() {
  19. MAP valueMap;
  20. valueMap["string"] = "abc";
  21. function(valueMap, "string");
  22. function(valueMap, "strong");
  23. return 0;
  24. }
Success #stdin #stdout 0s 15240KB
stdin
Standard input is empty
stdout
Value of key "string": abc
Key "strong" not found