std::filesystem:: relative, std::filesystem:: proximate
|
定义于头文件
<filesystem>
|
||
|
path relative
(
const
std::
filesystem
::
path
&
p,
std:: error_code & ec ) ; |
(1) | (C++17 起) |
|
path relative
(
const
std::
filesystem
::
path
&
p,
const std:: filesystem :: path & base = std:: filesystem :: current_path ( ) ) ; |
(2) | (C++17 起) |
|
path relative
(
const
std::
filesystem
::
path
&
p,
const
std::
filesystem
::
path
&
base,
|
(3) | (C++17 起) |
|
path proximate
(
const
std::
filesystem
::
path
&
p,
std:: error_code & ec ) ; |
(4) | (C++17 起) |
|
path proximate
(
const
std::
filesystem
::
path
&
p,
const std:: filesystem :: path & base = std:: filesystem :: current_path ( ) ) ; |
(5) | (C++17 起) |
|
path proximate
(
const
std::
filesystem
::
path
&
p,
const
std::
filesystem
::
path
&
base,
|
(6) | (C++17 起) |
目录 |
参数
| p | - | 现有路径 |
| base | - | 基础路径, p 将基于此路径生成相对/邻近路径 |
| ec | - | 用于存储错误状态的错误代码 |
返回值
异常
任何未标记为
noexcept
的重载在内存分配失败时可能抛出
std::bad_alloc
。
示例
#include <filesystem> #include <iostream> void show(std::filesystem::path x, std::filesystem::path y) { std::cout << "x:\t\t " << x << "\ny:\t\t " << y << '\n' << "relative(x, y): " << std::filesystem::relative(x, y) << '\n' << "proximate(x, y): " << std::filesystem::proximate(x, y) << "\n\n"; } int main() { show("/a/b/c", "/a/b"); show("/a/c", "/a/b"); show("c", "/a/b"); show("/a/b", "c"); }
可能的输出:
x: "/a/b/c" y: "/a/b" relative(x, y): "c" proximate(x, y): "c" x: "/a/c" y: "/a/b" relative(x, y): "../c" proximate(x, y): "../c" x: "c" y: "/a/b" relative(x, y): "" proximate(x, y): "c" x: "/a/b" y: "c" relative(x, y): "" proximate(x, y): "/a/b"
参见
|
(C++17)
|
表示路径
(类) |
|
(C++17)
|
组合绝对路径
(函数) |
|
(C++17)
|
组合规范路径
(函数) |
|
将路径转换为正规形式
将路径转换为相对形式 将路径转换为近似形式 (
std::filesystem::path
的公开成员函数)
|