std::filesystem::path::concat, std::filesystem::path::operator+=
來自 cppreference.com
< 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-qualified 的編碼字元型別(char、char8_t、(C++20 起)char16_t、char32_t 或 wchar_t)。
目錄 |
[編輯] 引數
p | - | 要附加的路徑 |
str | - | 要附加的字串或字串檢視 |
ptr | - | 指向要附加的以 null 結尾字串開頭的指標 |
x | - | 要附加的單個字元 |
source | - | std::basic_string、std::basic_string_view、以 null 結尾的多字元字串,或指向以 null 結尾的多字元序列(表示路徑名,可以是可移植格式或原生格式)的輸入迭代器 |
first, last | - | 指定表示路徑名的多字元序列的 LegacyInputIterator 對 |
型別要求 | ||
-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; // an empty path p1 += "var"; // does not insert a separator std::cout << R"("" + "var" --> )" << p1 << '\n'; p1 += "lib"; // does not insert a separator 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 的約束 |
已新增 |
[編輯] 另請參閱
用目錄分隔符向路徑追加元素 (public 成員函式) | |
(C++17) |
用目錄分隔符連線兩個路徑 (函式) |