0

我不知道如何使用 std::filesystem::path 来识别绝对路径是否包含一些相对目录,但仍然没有运气。我有以下代码和平:

const std::filesystem::path absolutePath = "/Users/user/Library/Preferences";
const std::filesystem::path relativePath = "Library/Preferences";
bool isSubDir = isSubDirectory(absolutePath, relativePath);

在这种情况下,isSubDirectory 应该返回 true。感谢任何帮助。

4

1 回答 1

0

std::filesystem::path类为第一个元素和最后一个元素提供迭代器。因此,您可以使用以下命令在绝对路径中搜索相对路径std::search

#include <algorithm>
#include <filesystem>

namespace fs = std::filesystem;

bool isSubDirectory(const fs::path& absolutePath, const fs::path& relativePath)
{
    auto it = std::search(absolutePath.begin(), absolutePath.end(), relativePath.begin(), relativePath.end());
    return it != absolutePath.end();
}
于 2020-11-17T11:47:34.313 回答