Share via


path 類別

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

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

語法

class path;

建構函式

建構函式 描述
path 建構 path

Typedefs

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

成員函式

成員函數 描述
append 將指定的序列附加至 mypath ,並視需要插入preferred_separator。
assign mypath以指定的序列取代 ,視需要轉換。
begin path::iterator傳回 ,如果存在,則傳回指定 pathname 中的第一個路徑專案。
c_str 傳回 中第一個字元的 mypath 指標。
clear mypath.clear()執行 。
compare 傳回比較值。
concat 視需要將指定的序列附加至 mypath 、已轉換的 (但未插入分隔符號)。
empty 傳回 mypath.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()
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 轉換儲存在 中的 mypath 序列。
swap swap(mypath, right.mypath)執行 。
u16string 將儲存在 中的 mypath 序列轉換成 UTF-16,並傳回儲存在 型 u16string 別 的物件中。
u32string 將儲存在 中的 mypath 序列轉換為 UTF-32,並傳回儲存在 型 u32string 別 的物件中。
u8string 將儲存在 中的 mypath 序列轉換為 UTF-8,並傳回儲存在 型 u8string 別 的物件中。
value_type 此類型描述主機作業系統偏好的路徑元素。
wstring 將 儲存在 mypath 中的序列轉換成主機系統 wchar_t 針對序列所偏好的編碼,並傳回儲存在 型 wstring 別 物件中的序列。

操作員

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

需求

標頭:<filesystem>

命名空間:std::experimental::filesystem

path::append

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

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

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

參數

source
指定的序列。

first
指定序列的開頭。

last
指定序列的結尾。

path::assign

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

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

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

const value_type& *c_str() const noexcept;

path::clear

mypath.clear()執行 。

void clear() noexcept;

path::compare

第一個函式會傳回 mypath.compare(pval.native())。 第二個函式會傳回 mypath.compare(str)。 第三個函式會傳 mypath.compare(ptr) 回 。

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

參數

pval
要比較的路徑。

str
要比較的字串。

ptr
要比較的指標。

path::concat

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

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

傳回 mypath.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::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;
   // ...
   };

備註

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

  1. 根名稱,如果有的話

  2. 根目錄,如果有的話

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

針對 pvalpath 別的物件:

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

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

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

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

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

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

path::make_preferred

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

path& make_preferred();

path::native

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

const string_type& native() const noexcept;

備註

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

在 Windows 11 上執行的下列範例中,泛型路徑字串為 c:/t/temp/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;

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

參數

right
path要複製到 的 path

source
來源 path

備註

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

path::operator+=

各種 concat 運算式。

path& operator+=(const path& right);
path& operator+=(const string_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_typeElem

source
新增的來源。

備註

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

  1. concat(right);

  2. concat(path(str));

  3. concat(ptr);

  4. concat(string_type(1, elem));

  5. concat(source);

  6. concat(path(basic_string<Elem>(1, elem)));

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

path::path

path以各種方式建構 。

path();

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

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

template <class Source>
path(const Source& source, const locale& loc);

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

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

參數

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

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

loc
指定的地區設定。

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

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

備註

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

myname()如果是 path()

對於 path(const path& right , 它是 myname(right.myname)

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

myname(source)如果是 template<class Source> path(const Source& source)

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

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

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

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 相對路徑元件,特別是移除 root_path().native() 之後的 myname 尾碼,以及任何後續的備援目錄分隔符號。 元件可能是空的。

path::remove_filename

移除檔案名。

path& remove_filename();

path::replace_extension

取代 的 myname 延伸模組。

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

參數

newext
新的延伸模組。

備註

首先,從 myname 移除尾碼 extension().native() 。 然後,如果 !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

轉換儲存在 中的 mypath 序列。

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) 成員函式會以下列方式轉換儲存 mypath 的序列:

  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>()

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

path::string_type

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

typedef basic_string<value_type> string_type;

path::swap

swap(mypath, right.mypath)執行 。

void swap(path& right) noexcept;

path::u16string

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

u16string u16string() const;

path::u32string

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

u32string u32string() const;

path::u8string

將儲存在 中的 mypath 序列轉換為 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

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

wstring wstring() const;

另請參閱

標頭檔參考