path 類別

類別 path 會儲存 類型 string_type的物件, myname 其用途為說明,適合做為pathname使用。 string_type是 的basic_string<value_type>同義字,其中 value_type 是 Windows 或 wchar_t POSIX 上的同義字char。

此處描述的path類別是標頭中的 <filesystem> C++17 std::filesystem::path 類別。

如需詳細資訊與程式碼範例,請參閱檔案系統巡覽 (C++)。

語法

class path;

建構函式

建構函式 描述
path 建構 path。

Typedefs

類型名稱 描述
const_iterator iterator 的同義字。
iterator 指定之 pathmyname元件的雙向常數反覆運算器。
string_type 此類型是 basic_string<value_type>的同義字。

Enumerations

類型名稱 描述
format 指定建構子的路徑名稱格式: auto_format、、 native_format或 generic_format。

成員函式

成員函數 描述
append 將指定的序列附加至 myname,並視需要插入preferred_separator。
assign myname以指定的序列取代 ,視需要轉換。
begin path::iterator傳回 ,如果存在,則傳回指定pathname中的第一個路徑專案。
c_str 傳回 中第一個字元的 myname指標。
clear myname.clear()執行 。
compare 傳回比較值。
concat 視需要將指定的序列附加至 myname、已轉換的 (但未插入分隔符)。
empty 傳回 myname.empty()。
end 傳回 型 iterator別的結尾序列反覆運算器。
extension 傳回的 filename()後綴。
filename 回傳 的 myname檔名組件,具體來說 empty() ? path() : *--end()是 。 元件可能是空的。
generic_string 傳回 this->string<Elem, Traits, Alloc>(al) 並 (在 Windows 下) 將任何反斜線轉換成正斜線。
generic_u16string 傳回 u16string() 並 (在 Windows 下) 將任何反斜線轉換成正斜線。
generic_u32string 傳回 u32string() 並 (在 Windows 下) 將任何反斜線轉換成正斜線。
generic_u8string 傳回 u8string() 並 (在 Windows 下) 將任何反斜線轉換成正斜線。
generic_wstring 傳回 wstring() 並 (在 Windows 下) 將任何反斜線轉換成正斜線。
has_extension 傳回 !extension().empty()。
has_filename 傳回 !filename().empty()。
has_parent_path 傳回 !parent_path().empty()。
has_relative_path 傳回 !relative_path().empty()。
has_root_directory 傳回 !root_directory().empty()。
has_root_name 傳回 !root_name().empty()。
has_root_path 傳回 !root_path().empty()。
has_stem 傳回 !stem().empty()。
is_absolute 針對 Windows,函式會傳 has_root_name() && has_root_directory()回 。 針對 POSIX,函式會傳 has_root_directory()回 。
is_relative 傳回 !is_absolute()。
lexically_normal 回報 myname 轉換為正常形式。
lexically_proximate 返回 lexically_relative(base),或 *this 當該結果為空時。
lexically_relative 相對於基底路徑所做的回本 myname ,僅使用詞彙(字串)分析。
make_preferred 視需要將每個分隔符轉換成 preferred_separator 。
native 傳回路徑的原生表示。
parent_path 傳回 的 myname父路徑元件。
preferred_separator 常數物件會提供慣用的字元分隔路徑元件,隨主機作業系統而異。
relative_path 傳回的 myname相對路徑元件。
remove_filename 拿掉檔名。
replace_extension 取代的 myname延伸模組。
replace_filename 取代檔名。
root_directory 傳回 的 myname根目錄元件。
root_name 傳回 的 myname根名稱元件。
root_path 傳回 的 myname根路徑元件。
stem 傳 stem 回的 myname元件。
string 轉換儲存在中的 myname序列。
swap swap(myname, right.myname)執行 。
u16string 將儲存在中的 myname 序列轉換成UTF-16,並傳回儲存在型 u16string別的物件中。
u32string 將儲存在中的 myname 序列轉換為UTF-32,並傳回儲存在型 u32string別的物件中。
u8string 將儲存在中的 myname 序列轉換為UTF-8,並傳回儲存在型 u8string別的物件中。
value_type 此類型描述主機作業系統偏好的路徑元素。
wstring 將 儲存在 myname 中的序列轉換成主機系統 wchar_t 針對序列所偏好的編碼,並傳回儲存在 型 wstring別物件中的序列。

操作員

運算子 描述
operator= 以另一個路徑的複本取代路徑的專案。
operator+= 各種 concat 表達式。
operator/= 各種 append 表達式。
operator string_type 傳回 myname。

需求

標頭:<filesystem>

命名空間:std::filesystem

path::append

將指定的序列附加至 myname,並視需要插入 preferred_separator 。

template <class Source>
path& append(const Source& source);

template <class InIt>
path& append(InIt first, InIt last);

參數

source
指定的序列。

first
指定序列的開頭。

last
指定序列的結尾。

path::assign

myname以指定的序列取代 ,視需要轉換。

path& assign(string_type&& source);

template <class Source>
path& assign(const Source& source);

template <class InIt>
path& assign(InIt first, InIt last);

參數

source
指定的序列。

first
指定序列的開頭。

last
指定序列的結尾。

path::begin

path::iterator傳回 ,如果存在,則傳回指定pathname中的第一個路徑專案。

iterator begin() const;

path::c_str

傳回 中第一個字元的 myname指標。

const value_type* c_str() const noexcept;

path::clear

myname.clear()執行 。

void clear() noexcept;

path::compare

第一個函數比較該路徑的元素與 pval。 其他函數則從其參數構造路徑,並回傳與該路徑比較後的結果。

int compare(const path& pval) const noexcept;
int compare(const string_type& str) const;
int compare(basic_string_view<value_type> str) const;
int compare(const value_type *ptr) const;

參數

pval
要比較的路徑。

str
要比較的字串。

ptr
要比較的指標。

path::concat

視需要將指定的序列附加至 myname、已轉換的 (但未插入分隔符)。

template <class Source>
path& concat(const Source& source);

template <class InIt>
path& concat(InIt first, InIt last);

參數

source
指定的序列。

first
指定序列的開頭。

last
指定序列的結尾。

path::const_iterator

iterator 的同義字。

typedef iterator const_iterator;

path::empty

傳回 myname.empty()。

bool empty() const noexcept;

path::end

傳回 型 iterator別的結尾序列反覆運算器。

iterator end() const;

path::extension

傳回的 filename()後綴。

path extension() const;

備註

傳回 的後綴 filename() X ,如下所示:

如果 X == path(".") || X == path("..") 或 如果 X 不包含點,則後綴是空的。

否則後置詞開頭 (並包含) 會是最右邊的點。

path::filename

回傳 的 myname檔名組件,具體來說 empty() ? path() : *--end()是 。 元件可能是空的。

path filename() const;

path::format

指定用於解釋傳遞給 path 建構子的字元序列的路徑名稱格式。

enum format {
    auto_format,
    native_format,
    generic_format
};

Members

列舉器 描述
auto_format 實作會自動偵測格式。 這是取 format 參數的建構子的預設。
native_format 以原生路徑名稱格式解讀序列。
generic_format 以通用路徑名稱格式解讀序列。

備註

由於原生格式與通用格式在 Windows 上可互換,Microsoft 實作將三種列舉器視為相同。 format此參數是為了標準一致性與可攜性而提供。 欲了解更多資訊,請參閱 WG21 N4659 [fs.class.path] 規範。

path::generic_string

傳回 this->string<Elem, Traits, Alloc>(al) 並 (在 Windows 下) 將任何反斜線轉換成正斜線。

template <class Elem,
    class Traits = char_traits<Elem>,
    class Alloc = allocator<Elem>>
  basic_string<Elem, Traits, Alloc>
    generic_string(const Alloc& al = Alloc()) const;

string generic_string() const;

path::generic_u16string

傳回 u16string() 並 (在 Windows 下) 將任何反斜線轉換成正斜線。

u16string generic_u16string() const;

path::generic_u32string

傳回 u32string() 並 (在 Windows 下) 將任何反斜線轉換成正斜線。

u32string generic_u32string() const;

path::generic_u8string

傳回 u8string() 並 (在 Windows 下) 將任何反斜線轉換成正斜線。

string generic_u8string() const;

path::generic_wstring

傳回 wstring() 並 (在 Windows 下) 將任何反斜線轉換成正斜線。

wstring generic_wstring() const;

path::has_extension

傳回 !extension().empty()。

bool has_extension() const;

path::has_filename

傳回 !filename().empty()。

bool has_filename() const;

path::has_parent_path

傳回 !parent_path().empty()。

bool has_parent_path() const;

path::has_relative_path

傳回 !relative_path().empty()。

bool has_relative_path() const;

path::has_root_directory

傳回 !root_directory().empty()。

bool has_root_directory() const;

path::has_root_name

傳回 !root_name().empty()。

bool has_root_name() const;

path::has_root_path

傳回 !root_path().empty()。

bool has_root_path() const;

path::has_stem

傳回 !stem().empty()。

bool has_stem() const;

path::is_absolute

針對 Windows,函式會傳 has_root_name() && has_root_directory()回 。 針對 POSIX,函式會傳 has_root_directory()回 。

bool is_absolute() const;

path::is_relative

傳回 !is_absolute()。

bool is_relative() const;

path::iterator

指定之路徑元件的 myname雙向常數反覆運算器。

class iterator
   {
   // bidirectional iterator for path
   typedef bidirectional_iterator_tag iterator_category;
   typedef path_type value_type;
   typedef ptrdiff_t difference_type;
   typedef const value_type *pointer;
   typedef const value_type& reference;
   // ...
   };

備註

類別描述指定序列 中 之 path 元件的雙向常數反覆運算器myname:

  1. 根名稱,如果有的話

  2. 根目錄,如果有的話

  3. 父 path代 的其餘目錄元素,如果有,則以檔名結尾,如果有

針對 pval 型 path別的物件:

  1. path::iterator X = pval.begin() 如果存在,則會指定pathname中的第一個專案 path 。

  2. X == pval.end() 是在 true 剛超過元件序列結尾的點時 X 。

  3. *X 傳回符合目前元件的字串

  4. 如果有的話,++X 會指定順序中的下一個元件。

  5. 如果有的話,--X 會指定順序中的上一個元件。

  6. myname改變 會使指定 中myname專案的所有反覆運算器失效。

path::lexically_normal

回報 myname 轉換為正常形式。 轉換純粹是詞彙層面:檔案系統不會被存取。

path lexically_normal() const;

備註

正規化會坍塌冗餘元素,如 . 和 .. 以及目錄分隔符。 例如,path("a/./b/../c").lexically_normal()在 Windows 上是 yieldsa\c。 空路徑會正規化為空路徑;路徑化簡為零後,正規化為 .。 完整正規化演算法請參見 WG21 N4659 [fs.path.generic] 規範。

path::lexically_proximate

回傳 的 lexically_relative(base)結果。 如果結果是空路徑,則會回傳一份 的 *this 副本。

path lexically_proximate(const path& base) const;

參數

base
要接近 *this 的基底路徑。

path::lexically_relative

相對於 base的回歸myname,僅使用詞彙(字串)分析。 檔案系統不會被存取。

path lexically_relative(const path& base) const;

參數

base
相對於該建立 *this 的基礎路徑。

備註

如果 root_name() != base.root_name(),或一條路徑是絕對路徑而另一條路徑是相對路徑,或路徑有檔名但沒有根目錄而基底沒有,則會回傳一個空路徑。 否則,透過尋找第一個不匹配元素,並對 .. 的base每個剩餘元素 ,再對 的剩餘元素 *this,來決定從 base 到 的*this相對路徑。 例如,path("/a/d").lexically_relative("/a/b/c") 會產生 ..\..\d。 完整演算法請參閱 WG21 N4659 [fs.path.gen] 規範。

path::make_preferred

視需要將每個分隔符轉換成 preferred_separator 。

path& make_preferred();

path::native

取得路徑的原生字串表示。

const string_type& native() const noexcept;

備註

路徑以可攜式泛型格式提供(請參閱 generic_string())或路徑的原生格式。 此函式會傳回原生字串。 在 POSIX 系統上,泛型格式和原生格式相同。

在以下在 Windows 上執行的範例中,通用路徑字串為 c:/t/temp.txt ,原生字串為 c:\t\temp.txt。

// Compile with /std:c++17 or higher
#include <filesystem>

int main()
{
    std::filesystem::path p(R"(c:\t\temp.txt)");
    auto native = p.native();          // Windows: L"c:\\t\\temp.txt"
    auto generic = p.generic_string(); // Windows: "c:/t/temp.txt"
}

path::operator=

以另一個路徑的複本取代路徑的專案。

path& operator=(const path& right);
path& operator=(path&& right) noexcept;
path& operator=(string_type&& source);

template <class Source>
path& operator=(const Source& source);

參數

right
path要複製到 的 path。

source
來源 path。

備註

第一個成員運算子會right.mynamemyname複製到 。 第二個成員運算子會移至 right.mynamemyname。 第三個成員運算子移動 source 到 myname。 第四個成員算符的行為與 *this = path(source)相同。

path::operator+=

各種 concat 表達式。

path& operator+=(const path& right);
path& operator+=(const string_type& str);
path& operator+=(basic_string_view<value_type> str);
path& operator+=(const value_type *ptr);
path& operator+=(value_type elem);

template <class Source>
path& operator+=(const Source& source);

template <class Elem>
path& operator+=(Elem elem);

參數

right
新增的路徑。

str
新增的字串。

ptr
已加入的指標。

elem
已加入 value_type 或 Elem。

source
新增的來源。

備註

成員函式的行為與 concat 使用參數呼叫相同。 path、 string_type、 basic_string_view和 null-終止value_type*的超載串接了參數的字元序列。 value_type和 Elem overload 是將單一字元串接起來的。 模板 Source 化的超載會將轉換後的來源序列串接起來。

path::operator/=

各種 append 表達式。

path& operator/=(const path& right);

template <class Source>
path& operator/=(const Source& source);

參數

right
新增的路徑。

source
新增的來源。

備註

成員函式的行為與下列對應的運算式相同:

  1. append(right);

  2. append(source);

path::operator string_type

傳回 myname。

operator string_type() const;

path::parent_path

傳回 的 myname父路徑元件。

path parent_path() const;

備註

傳回 的myname父路徑元件,特別是移除myname之後的 filename().native() 前置詞,以及任何緊接在目錄分隔符之前。 (同樣地,如果begin() != end()為 ,則會藉由連續套用 [begin(), --end())來合併範圍operator/=中的所有元素。元件可能是空的。

path::path

path以各種方式建構 。

path() noexcept;

path(const path& right);
path(path&& right) noexcept;

path(string_type&& source, format fmt = auto_format);

template <class Source>
path(const Source& source, format fmt = auto_format);

template <class InIt>
path(InIt first, InIt last, format fmt = auto_format);

template <class Source>
path(const Source& source, const locale& loc, format fmt = auto_format);

template <class InIt>
path(InIt first, InIt last, const locale& loc, format fmt = auto_format);

參數

right
建構路徑要作為複本的路徑。

source
建構路徑要作為複本的來源。

fmt
用來解釋的路徑名稱format或first/last範圍。source 預設為 auto_format。

loc
指定的地區設定。

first
要複製之第一個元素的位置。

last
要複製之最後一個專案的位置。

備註

建構函式會以各種方式建構 myname :

path()如果是 myname() 。

path(const path& right)如果是 myname(right.myname) 。

path(path&& right)如果是 myname(right.myname) 。

path(string_type&& source, format fmt)如果是 myname(move(source)) 。

template<class Source> path(const Source& source, format fmt)如果是 myname(source) 。

template<class Source> path(const Source& source, const locale& loc, format fmt)如果是 myname(source) ,請從 codecvt取得任何所需的 loc Facet。

template<class InIt> path(InIt first, InIt last, format fmt)如果是 myname(first, last) 。

template<class InIt> path(InIt first, InIt last, const locale& loc, format fmt)如果是 myname(first, last) ,請從 codecvt取得任何所需的 loc Facet。

參數選擇 fmt 路徑名稱格式。 在 Windows 上,原生與通用格式可互換,因此 Microsoft 實作忽略fmt了 。

path::preferred_separator

常數物件會提供慣用的字元分隔路徑元件,隨主機作業系統而異。

#if _WIN32_C_LIB
static constexpr value_type preferred_separator = L'\\';
#else // assume POSIX
static constexpr value_type preferred_separator = '/';
#endif // filesystem model now defined

備註

在 Windows L'/' 底下的大部分內容中,它同樣允許使用它的位置。

path::relative_path

傳回的 myname相對路徑元件。

path relative_path() const;

備註

傳回的myname相對路徑元件,特別是移除myname之後的 root_path().native() 後綴,以及任何後續的備援目錄分隔符。 元件可能是空的。

path::remove_filename

拿掉檔名。

path& remove_filename();

path::replace_extension

取代的 myname延伸模組。

path& replace_extension(const path& newext = path());

參數

newext
新的延伸模組。

備註

首先,從 extension().native()移除後綴myname。 然後,如果 !newext.empty() && newext[0] != dot (其中 dot 為 *path(".").c_str()),則 點 會附加至 myname。 然後將 newext 附加至 myname。

path::replace_filename

取代檔名。

path& replace_filename(const path& pval);

參數

pval
檔名的路徑。

備註

成員函式會執行:

remove_filename();

*this /= pval;
return (*this);

path::root_directory

傳回 的 myname根目錄元件。

path root_directory() const;

備註

元件可能是空的。

path::root_name

傳回 的 myname根名稱元件。

path root_name() const;

備註

元件可能是空的。

path::root_path

傳回 的 myname根路徑元件。

path root_path() const;

備註

傳回 的 myname根路徑元件,特別是 root_name() / root_directory 。 元件可能是空的。

path::stem

傳 stem 回的 myname元件。

path stem() const;

備註

傳 stem 回 的 myname元件,特別是 filename().native() 移除任何尾端 extension().native() 的元件。 元件可能是空的。

path::string

轉換儲存在中的 myname序列。

template <class Elem, class Traits = char_traits<Elem>, class Alloc = allocator<Elem>>
basic_string<Elem, Traits, Alloc> string(const Alloc& al = Alloc()) const;
string string() const;

備註

第一個 (template) 成員函式會以下列方式轉換儲存 myname 的序列:

  1. string() 的 string<char, Traits, Alloc>()

  2. wstring() 的 string<wchar_t, Traits, Alloc>()

  3. u16string() 的 string<char16_t, Traits, Alloc>()

  4. u32string() 的 string<char32_t, Traits, Alloc>()

第二個成員函式會將儲存在 中的 myname 序列轉換成主機系統 char 偏好的編碼,並傳回儲存在 型 string別 物件中的序列。

path::string_type

此類型是 basic_string<value_type>的同義字。

typedef basic_string<value_type> string_type;

path::swap

swap(myname, right.myname)執行 。

void swap(path& right) noexcept;

path::u16string

將儲存在中的 myname 序列轉換成UTF-16,並傳回儲存在型 u16string別的物件中。

u16string u16string() const;

path::u32string

將儲存在中的 myname 序列轉換為UTF-32,並傳回儲存在型 u32string別的物件中。

u32string u32string() const;

path::u8string

將儲存在中的 myname 序列轉換為UTF-8,並傳回儲存在型 u8string別的物件中。

string u8string() const;

path::value_type

此類型描述 path 主機作業系統偏好的專案。

#if _WIN32_C_LIB
typedef wchar_t value_type;
#else // assume POSIX
typedef char value_type;
#endif // filesystem model now defined

path::wstring

將 儲存在 myname 中的序列轉換成主機系統 wchar_t 針對序列所偏好的編碼,並傳回儲存在 型 wstring別物件中的序列。

wstring wstring() const;

另請參閱

<filesystem>
標頭檔參考