所以我们有一组文件名\url,如file、folder/file、folder/file2、folder/file3、folder/folder2/fileN
等。我们得到一个字符串,如文件夹/
。我们想找到folder/file
、folder/file2
、folder/file3
,最有趣的是folder/folder2/
(我们不想列出 forlder2 的内容只是表明它存在并且可以搜索到)。通过 STL 和 Boost 可以实现这样的事情吗?如何实现?
Ups - 刚发现我前一段时间已经找到过这个了here ...但还没有找到正确答案...
最佳答案
相对简单的 C++11 实现。这可以很容易地修改为 C++03。 (警告:还没有编译或测试这个)。
std::set<std::string> urls; // The set of values you have
std::string key_search = "folder/"; // text to search for
std::for_each(
urls.begin(),
urls.end(),
[&key_search] (const std::string& value)
{
// use std::string::find, this will only display
// strings that match from the beginning of the
// stored value:
if(0 == value.find(key_search))
std::cout << value << "\n"; // display
});
关于c++ - 有一个只有文件名(a、f/a、f/b、f/f/c 等)的 std::set 如何通过给定的 f/列出目录?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7717182/