std::filesystem::path:: append, std::filesystem::path:: operator/=
From cppreference.net
<
cpp
|
filesystem
|
path
|
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 起) |
1)
若
p.
is_absolute
(
)
||
(
p.
has_root_name
(
)
&&
p.
root_name
(
)
!
=
root_name
(
)
)
,则通过
operator
=
(
p
)
方式将当前路径替换为
p
并结束操作。
* 否则,若
p.
has_root_directory
(
)
,则从
*
this
的通用格式路径名中移除所有根目录及完整相对路径。
* 否则,若
has_filename
(
)
||
(
!
has_root_directory
(
)
&&
is_absolute
(
)
)
,则向
*
this
的通用格式追加
path::preferred_separator
。
* 无论哪种方式,都会将
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 的根名称后追加)
2,3)
与
(1)
相同,但接受任意
std::basic_string
、
std::basic_string_view
、以空字符结尾的多字符字符串,或指向以空字符结尾的多字符序列的输入迭代器。等效于
return
operator
/
=
(
path
(
source
)
)
;
。
4)
与
(1)
相同,但接受任何指定多字符字符串的迭代器对。等效于
return
operator
/
=
(
path
(
first, last
)
)
;
。
(2)
和
(3)
仅在
Source
与
path
类型不同,且满足以下任一条件时参与重载决议:
-
Source是 std::basic_string 或 std::basic_string_view 的特化,或 - std:: iterator_traits < std:: decay_t < Source >> :: value_type 有效且表示可能为 const 限定的编码字符类型( char 、 char8_t 、 (C++20 起) char16_t 、 char32_t 或 wchar_t )。
目录 |
参数
| p | - | 要追加的路径名 |
| source | - | std::basic_string 、 std::basic_string_view 、以空字符结尾的多字节字符串,或指向以空字符结尾的多字节序列的输入迭代器,用于表示路径名(可移植格式或原生格式均可) |
| first, last | - | 一对 LegacyInputIterator s ,用于指定表示路径名的多字节序列 |
| 类型要求 | ||
-
InputIt
必须满足
LegacyInputIterator
的要求。
|
||
-
InputIt
的值类型必须是已编码字符类型之一(
char
、
wchar_t
、
char16_t
和
char32_t
)。
|
||
返回值
* this
异常
若内存分配失败,可能抛出 std:: bad_alloc 。
注释
这些函数能有效生成参数路径 p 在特定环境下的近似语义解释,该环境以 * this 作为起始目录。
示例
该输出是在Windows系统上生成的。
运行此代码
#include <filesystem> #include <iostream> namespace fs = std::filesystem; int main() { fs::path p1 = "C:"; p1 /= "Users"; // does not insert a separator std::cout << "\"C:\" / \"Users\" == " << p1 << '\n'; p1 /= "batman"; // inserts fs::path::preferred_separator, '\' on Windows std::cout << "\"C:\" / \"Users\" / \"batman\" == " << p1 << '\n'; }
可能的输出:
"C:" / "Users" == "C:Users" "C:" / "Users" / "batman" == "C:Users\\batman"
缺陷报告
以下行为变更缺陷报告被追溯应用于先前发布的C++标准。
| 缺陷报告 | 适用范围 | 发布时行为 | 正确行为 |
|---|---|---|---|
| LWG 3244 | C++17 |
缺失
Source
不能是
path
的约束条件
|
已添加 |
参见
|
在不引入目录分隔符的情况下连接两个路径
(公开成员函数) |
|
|
(C++17)
|
使用目录分隔符连接两个路径
(函数) |