path 类存储类型为 string_type 的对象,在这里出于阐释目的将其称为 myname,它适合用作路径名称。
string_type 是 basic_string<value_type> 的同义词,其中在 Windows 上,value_type 是 wchar_t 的同义词,在 POSIX 上,它是 char 的同义词。
此处介绍的path类是标头中的 <filesystem> C++17 std::filesystem::path 类。
有关详细信息和代码示例,请参阅文件系统导航 (C++)。
语法
class path;
构造函数
| 构造函数 | 说明 |
|---|---|
path |
构造一个 path。 |
Typedef
| 类型名称 | 说明 |
|---|---|
const_iterator |
iterator 的同义词。 |
iterator |
一个双向常量迭代器,用于指定 path 的 myname 组件。 |
string_type |
类型是 basic_string<value_type> 的同义词。 |
Enumerations
| 类型名称 | 说明 |
|---|---|
format |
指定构造函数的路径名称格式: auto_format、 native_format或 generic_format。 |
成员函数
| 成员函数 | 说明 |
|---|---|
append |
将指定序列追加到 myname(按需进行转将换和插入 preferred_separator)。 |
assign |
将 myname 替换为指定的序列(按需进行转换)。 |
begin |
返回 path::iterator,它指示路径名称中的第一个路径元素(若存在)。 |
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,它指示路径名称中的第一个路径元素(若存在)。
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 |
以本机 pathname 格式解释序列。 |
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 组件:
根名称(如存在)
根目录(如存在)
父级
path的剩余目录元素(如存在),它们以 filename 结尾(如存在)
对于 pval,path 类型的对象:
path::iterator X = pval.begin()指定 pathname 中的第一个path元素(若存在)。如果
X == pval.end()点刚超出组件序列的末尾,true为X。*X会返回与当前组件匹配的字符串++X指定序列中的下一个组件(如果存在)。--X指定序列中的前面的组件(如果存在)。更改
myname会使所有指定myname中的元素的迭代器无效。
path::lexically_normal
返回 myname 转换为普通窗体。 转换纯粹是词法:无法访问文件系统。
path lexically_normal() const;
备注
规范化折叠冗余元素,例如 . 和 .. 目录分隔符。 例如, path("a/./b/../c").lexically_normal() Windows的生成a\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
myname仅使用词法(字符串)分析进行相对于base的返回。 无法访问文件系统。
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.myname 复制到 myname。 第二个成员运算符将 right.myname 移动到 myname。 第三个成员运算符进入 sourcemyname。 第四个成员运算符的行为与 *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_typebasic_string_viewnull 结尾value_type*的重载连接参数的字符序列。
value_type和Elem重载连接单个字符。 模板化 Source 重载连接转换后的源序列。
path::operator/=
各种 append 表达式。
path& operator/=(const path& right);
template <class Source>
path& operator/=(const Source& source);
参数
right
添加的路径。
source
添加的源。
备注
成员函数的行为与以下相应表达式相同:
append(right);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
用于解释source或first/last范围的路径名。format 默认值为 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 参数选择 pathname 格式。 在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;
备注
第一个(模板)成员函数按如下方式转换 myname 中存储的序列:
若
string(),表示集string<char, Traits, Alloc>()若
wstring(),表示集string<wchar_t, Traits, Alloc>()若
u16string(),表示集string<char16_t, Traits, Alloc>()若
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;