std::filesystem::path:: concat, std::filesystem::path:: operator+=
From cppreference.net
<
cpp
|
filesystem
|
path
|
path
&
operator
+
=
(
const
path
&
p
)
;
|
(1) | (C++17 起) |
|
path
&
operator
+
=
(
const
string_type
&
str
)
;
path & operator + = ( std:: basic_string_view < value_type > str ) ; |
(2) | (C++17 起) |
|
path
&
operator
+
=
(
const
value_type
*
ptr
)
;
|
(3) | (C++17 起) |
|
path
&
operator
+
=
(
value_type x
)
;
|
(4) | (C++17 起) |
|
template
<
class
CharT
>
path & operator + = ( CharT x ) ; |
(5) | (C++17 起) |
|
template
<
class
Source
>
path & operator + = ( const Source & source ) ; |
(6) | (C++17 起) |
|
template
<
class
Source
>
path & concat ( const Source & source ) ; |
(7) | (C++17 起) |
|
template
<
class
InputIt
>
path & concat ( InputIt first, InputIt last ) ; |
(8) | (C++17 起) |
将当前路径与参数进行拼接
1-3,6,7)
将以原生格式存储的
path
(
p
)
.
native
(
)
追加到
*
this
中存储的路径名。这会直接操作
native
(
)
的值,并且可能在不同操作系统之间缺乏可移植性。
4,5)
等同于
return
*
this
+
=
std::
basic_string_view
(
&
x,
1
)
;
。
8)
等同于
return
*
this
+
=
path
(
first, last
)
;
。
(6)
和
(7)
仅在
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 | - | 要追加的路径 |
| str | - | 要追加的字符串或字符串视图 |
| ptr | - | 要追加的以空字符结尾的字符串起始指针 |
| x | - | 要追加的单个字符 |
| source | - | std::basic_string 、 std::basic_string_view 、以空字符结尾的多字符字符串,或指向以空字符结尾的多字符序列的输入迭代器,用于表示路径名(可移植格式或原生格式) |
| first, last | - | 指定表示路径名的多字符序列的 LegacyInputIterator s 迭代器对 |
| 类型要求 | ||
-
InputIt
必须满足
LegacyInputIterator
的要求。
|
||
-
InputIt
的值类型必须是已编码字符类型之一(
char
、
wchar_t
、
char16_t
和
char32_t
)。
|
||
-
CharT
必须是已编码字符类型之一(
char
、
wchar_t
、
char16_t
和
char32_t
)。
|
||
返回值
* this
异常
若内存分配失败,可能抛出 std:: bad_alloc 。
注释
与 append() 或 operator/= 不同,此操作永远不会引入额外的目录分隔符。
示例
运行此代码
#include <filesystem> #include <iostream> #include <string> int main() { std::filesystem::path p1; // 空路径 p1 += "var"; // 不插入分隔符 std::cout << R"("" + "var" --> )" << p1 << '\n'; p1 += "lib"; // 不插入分隔符 std::cout << R"("var" + "lib" --> )" << p1 << '\n'; auto str = std::string{"1234567"}; p1.concat(std::begin(str) + 3, std::end(str) - 1); std::cout << "p1.concat --> " << p1 << '\n'; }
输出:
"" + "var" --> "var" "var" + "lib" --> "varlib" p1.concat --> "varlib456"
缺陷报告
以下行为变更缺陷报告被追溯应用于先前发布的C++标准。
| 缺陷报告 | 适用范围 | 发布时的行为 | 正确行为 |
|---|---|---|---|
| LWG 3055 | C++17 | 连接单个字符的规范存在格式错误 | 修正为格式正确 |
| LWG 3244 | C++17 |
缺失
Source
不能是
path
的约束条件
|
已添加 |
参见
|
使用目录分隔符向路径追加元素
(公开成员函数) |
|
|
(C++17)
|
使用目录分隔符合并两个路径
(函数) |