首页 > 解决方案 > 检查路径是否包含 C++ 中的另一个

问题描述

我正在寻求实现类似的目标

if (basePath.contains(subPath)) {
    // subPath 是 basePath 的一个 subPath
}

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

std办法吗?


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

标签: c++std

解决方案


您可以遍历两个路径中的项目:

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

推荐阅读