fork(2) download
  1. #include <algorithm>
  2. #include <iostream>
  3. #include <string>
  4. #include <vector>
  5.  
  6. void test(const std::string& prefix, const std::vector<std::string>& v)
  7. {
  8. std::cout << "Testing prefix: " << prefix << "\n";
  9. auto r = std::equal_range(v.begin(), v.end(), prefix, [&prefix](const std::string& lhs, const std::string& rhs)
  10. {
  11. bool ret = false;
  12. if (rhs == prefix)
  13. {
  14. ret = lhs.substr(0, prefix.length()) < prefix;
  15. }
  16. if (lhs == prefix)
  17. {
  18. ret = prefix < rhs.substr(0, prefix.length());
  19. }
  20. return ret;
  21. });
  22. for (auto it = r.first; it != r.second; ++it)
  23. {
  24. std::cout << *it << "\n";
  25. }
  26. std::cout << "\n";
  27. }
  28.  
  29. int main()
  30. {
  31. const std::vector<std::string> v =
  32. {
  33. "c:/users/andy/documents/screenshot.jpg",
  34. "c:/users/bob/desktop/file.txt",
  35. "c:/users/bob/desktop/picture.png",
  36. "c:/users/bob/desktop/video.mp4",
  37. "c:/users/john/desktop/note.txt",
  38. };
  39.  
  40. test("c:/users/bob", v);
  41. test("c:/users", v);
  42. test("d", v);
  43. test("c:/users/bob/desktop/picture.png", v);
  44.  
  45. return 0;
  46. }
  47.  
Success #stdin #stdout 0s 4548KB
stdin
Standard input is empty
stdout
Testing prefix: c:/users/bob
c:/users/bob/desktop/file.txt
c:/users/bob/desktop/picture.png
c:/users/bob/desktop/video.mp4

Testing prefix: c:/users
c:/users/andy/documents/screenshot.jpg
c:/users/bob/desktop/file.txt
c:/users/bob/desktop/picture.png
c:/users/bob/desktop/video.mp4
c:/users/john/desktop/note.txt

Testing prefix: d

Testing prefix: c:/users/bob/desktop/picture.png
c:/users/bob/desktop/picture.png