fork download
  1. #include <iostream>
  2. #include <sstream>
  3. #include <vector>
  4. #include <string>
  5. #include <vector>
  6. #include <algorithm>
  7.  
  8. std::vector<std::string> extractTokens(std::istream& is)
  9. {
  10. std::vector<std::string> tokens;
  11. std::string token;
  12.  
  13. while (is >> token)
  14. tokens.push_back(token);
  15.  
  16. return tokens;
  17. }
  18.  
  19. int main()
  20. {
  21. std::istringstream is
  22. {
  23. "Brianna\n"
  24. "Lindsey\n"
  25. "Paul\n"
  26. "Lisa\n"
  27. };
  28.  
  29. auto names = extractTokens(is);
  30. std::sort(names.begin(), names.end());
  31.  
  32.  
  33. std::vector<std::string> patterns = { "Li", "Br", "Lin", "P", "Zi", "Au", "i" };
  34.  
  35. for (auto& pattern : patterns)
  36. {
  37. auto it = std::lower_bound(names.begin(), names.end(), pattern);
  38.  
  39. std::cout << "Results for \"" << pattern << "\" are:\n";
  40.  
  41. if (it != names.end() && it->find(pattern) == 0)
  42. {
  43. while (it != names.end() && it->find(pattern) == 0)
  44. std::cout << '\t' << *it++ << '\n';
  45. }
  46. else
  47. std::cout << "\tNo results.\n";
  48. std::cout << '\n';
  49. }
  50. }
Success #stdin #stdout 0s 3436KB
stdin
Standard input is empty
stdout
Results for "Li" are:
	Lindsey
	Lisa

Results for "Br" are:
	Brianna

Results for "Lin" are:
	Lindsey

Results for "P" are:
	Paul

Results for "Zi" are:
	No results.

Results for "Au" are:
	No results.

Results for "i" are:
	No results.