#include <algorithm>
#include <iostream>
#include <string>
#include <vector>
void test(const std::string& prefix, const std::vector<std::string>& v)
{
std::cout << "Testing prefix: " << prefix << "\n";
auto r = std::equal_range(v.begin(), v.end(), prefix, [&prefix](const std::string& lhs, const std::string& rhs)
{
bool ret = false;
if (rhs == prefix)
{
ret = lhs.substr(0, prefix.length()) < prefix;
}
if (lhs == prefix)
{
ret = prefix < rhs.substr(0, prefix.length());
}
return ret;
});
for (auto it = r.first; it != r.second; ++it)
{
std::cout << *it << "\n";
}
std::cout << "\n";
}
int main()
{
const std::vector<std::string> v =
{
"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",
};
test("c:/users/bob", v);
test("c:/users", v);
test("d", v);
test("c:/users/bob/desktop/picture.png", v);
return 0;
}
I2luY2x1ZGUgPGFsZ29yaXRobT4KI2luY2x1ZGUgPGlvc3RyZWFtPgojaW5jbHVkZSA8c3RyaW5nPgojaW5jbHVkZSA8dmVjdG9yPgoKdm9pZCB0ZXN0KGNvbnN0IHN0ZDo6c3RyaW5nJiBwcmVmaXgsIGNvbnN0IHN0ZDo6dmVjdG9yPHN0ZDo6c3RyaW5nPiYgdikKewoJc3RkOjpjb3V0IDw8ICJUZXN0aW5nIHByZWZpeDogIiA8PCBwcmVmaXggPDwgIlxuIjsKCWF1dG8gciA9IHN0ZDo6ZXF1YWxfcmFuZ2Uodi5iZWdpbigpLCB2LmVuZCgpLCBwcmVmaXgsIFsmcHJlZml4XShjb25zdCBzdGQ6OnN0cmluZyYgbGhzLCBjb25zdCBzdGQ6OnN0cmluZyYgcmhzKQoJewoJCWJvb2wgcmV0ID0gZmFsc2U7CgkJaWYgKHJocyA9PSBwcmVmaXgpCgkJewoJCQlyZXQgPSBsaHMuc3Vic3RyKDAsIHByZWZpeC5sZW5ndGgoKSkgPCBwcmVmaXg7CgkJfQoJCWlmIChsaHMgPT0gcH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