检查路径是否包含 C++ 中的另一个路径

Tob*_*emi 6 c++ std

我希望实现类似的目标

if (basePath.contains(subPath)) {
    // subPath is a subPath of the basePath
}
Run Code Online (Sandbox Code Playgroud)

我知道我可以通过遍历 的父母并在途中subPath检查来实现这一点。basePath

有没有std办法呢?


std::filesystem::path("/a/b/").contains("/a/b/c/d") == true

Nul*_*ull 0

您可以迭代两个路径中的项目:

for (auto b = basePath.begin(), s = subPath.begin(); b != basePath.end(); ++b, ++s)
{
    if (s == subPath.end() || *s != *b)
    {
        return false;
    }
}
return true;
Run Code Online (Sandbox Code Playgroud)