fork(3) download
  1. #include <map>
  2. #include <string>
  3. #include <iostream>
  4.  
  5. int main() {
  6. std::map<std::string, std::string> myMap{
  7. {"John", "AA"}, {"Mary", "BBB"}, {"Mother", "A"}, {"Marlon", "C"}, {"Marla", "D"}
  8. };
  9. std::string prefix("Marl");
  10.  
  11. std::cout << "First match:" << std::endl;
  12.  
  13. auto it = myMap.lower_bound(prefix);
  14. if (it != std::end(myMap) && it->first.compare(0, prefix.size(), prefix) == 0)
  15. std::cout << it->first << ": " << it->second << std::endl;
  16.  
  17. std::cout << std::endl << "All matches:" << std::endl;
  18.  
  19. for (it = myMap.lower_bound(prefix); it != std::end(myMap) && it->first.compare(0, prefix.size(), prefix) == 0; ++it)
  20. std::cout << it->first << ": " << it->second << std::endl;
  21.  
  22. return 0;
  23. }
Success #stdin #stdout 0s 4460KB
stdin
Standard input is empty
stdout
First match:
Marla: D

All matches:
Marla: D
Marlon: C