c++ - C++ 实验文件系统没有“相对”功能?
问题描述
解决方案
You have to use third-party implementation (for ex. boost) or provide your own, something like (no complete solution - some extra edge cases need to be handled):
path relative(path p, path base)
{
// 1. convert p and base to absolute paths
p = fs::absolute(p);
base = fs::absolute(base);
// 2. find first mismatch and shared root path
auto mismatched = std::mismatch(p.begin(), p.end(), base.begin(), base.end());
// 3. if no mismatch return "."
if (mismatched.first == p.end() && mismatched.second == base.end())
return ".";
auto it_p = mismatched.first;
auto it_base = mismatched.second;
path ret;
// 4. iterate abase to the shared root and append "../"
for (; it_base != base.end(); ++it_base) ret /= "..";
// 5. iterate from the shared root to the p and append its parts
for (; it_p != p.end(); ++it_p) ret /= *it_p;
return ret;
}
推荐阅读
- flutter - 列小部件只是堆叠而不是可滚动的
- sql - 嵌套表 BigQuery 中的情况
- ios - Xcode中的断点设置
- c# - 如何在 C# 中打印多个页面?使用数据网格视图
- visual-studio-code - VS Code Git 扩展从选项中提取
- vba - Libreoffice 如何将 Cell.CellAddress.Row (已经是一个数字)转换为整数?
- javascript - 如何通过单击 ReactJs 的按钮来呈现 DIV 元素
- flutter - 如果 BannerAd 是在 Flutter 中的 Listview.builder 中创建的,如何处理它们?
- c++ - Qt:使调整大小事件不移动场景中的对象
- php - 在php中使用foreach循环获取下一个元素值