我想使用stl :: set來放置一些目錄路徑。 該集有一些特殊的目錄路徑,我把它。 我應該找到一些輸入路徑的特殊父項。如何使用std :: map或set來管理目錄的路徑?
有代碼。我評論了一些觀點。
set<wstring> special_directories;
void WhoIsMySpecialParent(const wstring& f)
{
set<wstring>::const_iterator it;
it = special_directories.upper_bound(f);
if (it == special_directories.begin())
{
printf("There isn't any special parent.");
return;
}
--it;
wprintf(L"The special parent is <%s>\n", it->c_str());
}
int _tmain(int argc, _TCHAR* argv[])
{
// These are special directories which I will manage.
// "/home" and "/home/benjamin" are not included in the special directories.
// If I want to make "/home" as special directory,
// I have to add "/home" in the set.
special_directories.insert(L"/");
special_directories.insert(L"/bin");
special_directories.insert(L"/etc");
special_directories.insert(L"/home/benjamin/documents");
special_directories.insert(L"/var");
special_directories.insert(L"/var/log");
WhoIsMySpecialParent(L"/bin/ls"); // Okay. It prints /bin
WhoIsMySpecialParent(L"/var/log/aaaa"); // Okay. It prints /var/log
WhoIsMySpecialParent(L"/var/apache2"); // Okay. It prints /var
WhoIsMySpecialParent(L"/bz");
// Wrong. It prints "/bin". It should print "/"
WhoIsMySpecialParent(L"/home/benjamin");
// Wrong. It prints "/etc", It should print "/"
return 0;
}
我認爲這可以用upper_bound處理。但我可能是錯的。
您覺得如何?我應該放棄使用std :: set嗎?
如果你是我,你會如何解決這個問題?請任何想法。
+1好答案。謝謝。 – Benjamin