path& operator/=(const path& p);
|
(1) | (C++17 起) |
template< class Source >
path& operator/=( const Source& source ); |
(2) | (C++17 起) |
template< class Source >
path& append( const Source& source ); |
(3) | (C++17 起) |
template< class InputIt >
path& append( InputIt first, InputIt last ); |
(4) | (C++17 起) |
path::preferred_separator
到 *this 的通用格式p
的原生格式路径名,从其通用格式忽略任何 root-name ,到 *this 的原生格式。// 其中 "//host" 是根名 path("//host") / "foo" // 结果是 "//host/foo" (附加分隔符) path("//host/") / "foo" // 结果亦为 "//host/foo" (附加,无分隔符) // POSIX 上, path("foo") / "" // 结果是 "foo/" (附加) path("foo") / "/bar"; // 结果是 "/bar" (替换) // Windows 上, path("foo") / "C:/bar"; // 结果是 "C:/bar" (替换) path("foo") / "C:"; // 结果是 "C:" (替换) path("C:") / ""; // 结果是 "C:" (附加,不带分隔符) path("C:foo") / "/bar"; // 生成 "C:/bar" (移除相对路径后附加) path("C:foo") / "C:bar"; // 生成 "C:foo/bar" (附加,忽略 p 的根名)
目录 |
p | - | 要添加的路径 |
source | - | std::basic_string 、 std::basic_string_view 、空终止多字节/宽字符串、或指向空终止多字符序列的输入迭代器,它表示路径名(以可移植或原生格式) |
first, last | - | 一对指定表示路径名的多字符序列的输入迭代器 (InputIterator )
|
类型要求 | ||
-
InputIt 必须满足 InputIterator 的要求。
|
||
-
InputIt 的 value_type 必须是编码字符( char 、 wchar_t 、 char16_t 及 char32_t )类型之一
|
*this
若内存分配失败则可能抛出 std::bad_alloc 。
#include <iostream> #include <filesystem> namespace fs = std::filesystem; int main() { fs::path p1 = "C:"; p1 /= "Users"; // 不插入分隔符 std::cout << "\"C:\" / \"Users\" == " << p1 << '\n'; p1 /= "batman"; // 插入 fs::path::preferred_separator ,在 Windows 上为 '\' std::cout << "\"C:\" / \"Users\" / \"batman\" == " << p1 << '\n'; }
可能的输出:
"C:" / "Users" == "C:Users" "C:" / "Users" / "batman" == "C:Users\batman"
连接二个路径而不加入目录分隔符 (公开成员函数) |
|
用目录分隔符连接二个路径 (函数) |