命名空间
变体
操作

std::filesystem::path::generic_string, std::filesystem::path::generic_wstring, std::filesystem::path::generic_u8string, std::filesystem::path::generic_u16string, std::filesystem::path::generic_u32string

来自 cppreference.cn
< cpp‎ | filesystem‎ | path
 
 
 
 
template< class CharT, class Traits = std::char_traits<CharT>,

          class Alloc = std::allocator<CharT> >
std::basic_string<CharT,Traits,Alloc>

    generic_string( const Alloc& a = Alloc() ) const;
(1) (since C++17)
(2) (since C++17)
std::string generic_string() const;
std::wstring generic_wstring() const;
std::u16string generic_u16string() const;
std::u32string generic_u32string() const;
(3)
std::string generic_u8string() const;
(since C++17)
(until C++20)
std::u8string generic_u8string() const;
(since C++20)

返回以通用路径名格式表示的内部路径名,并转换为特定的字符串类型。转换(如果存在)按如下方式指定

  • 如果 path::value_typechar,则转换(如果存在)是系统相关的。这在典型的 POSIX 系统(如 Linux)中是这种情况,其中原生编码为 UTF-8,而 string() 不执行任何转换。
  • 否则,如果 path::value_typewchar_t,则转换(如果存在)是未指定的。在 Windows 上是这种情况,其中 wchar_t 是 16 位,原生编码为 UTF-16。
  • 否则,如果 path::value_typechar16_t,则原生编码为 UTF-16,转换方法未指定。
  • 否则,如果 path::value_typechar32_t,则原生编码为 UTF-32,转换方法未指定。
  • 否则,如果 path::value_typechar8_t,则原生编码为 UTF-8,转换方法未指定。

/ 字符用作目录分隔符。

1) 所有内存分配均由 a 执行。
3)u8string() 的情况下,结果编码始终为 UTF-8。

目录

[编辑] 参数

a - 用于构造字符串的分配器
类型要求
-
CharT 必须是编码字符类型之一(char, wchar_t, char8_t(since C++20), char16_tchar32_t)。

[编辑] 返回值

以通用路径名格式表示的内部路径名,并转换为指定的字符串类型。

[编辑] 异常

可能抛出实现定义的异常。

[编辑] 示例

#include <cstddef>
#include <filesystem>
#include <iomanip>
#include <iostream>
#include <span>
#include <string_view>
 
void print(std::string_view rem, auto const& str)
{
    std::cout << rem << std::hex << std::uppercase << std::setfill('0');
    for (const auto b : std::as_bytes(std::span{str}))
        std::cout << std::setw(2) << std::to_integer<unsigned>(b) << ' ';
    std::cout << '\n';
}
 
int main()
{
    std::filesystem::path p{"/家/屋"};
    std::cout << p << '\n';
 
    print("string    : ", p.generic_string());
    print("u8string  : ", p.generic_u8string());
    print("u16string : ", p.generic_u16string());
    print("u32string : ", p.generic_u32string());
    print("wstring   : ", p.generic_wstring());
}

可能的输出

"/家/屋"
string    : 2F E5 AE B6 2F E5 B1 8B
u8string  : 2F E5 AE B6 2F E5 B1 8B
u16string : 2F 00 B6 5B 2F 00 4B 5C
u32string : 2F 00 00 00 B6 5B 00 00 2F 00 00 00 4B 5C 00 00
wstring   : 2F 00 00 00 B6 5B 00 00 2F 00 00 00 4B 5C 00 00

[编辑] 参见

返回以原生路径名格式表示的路径,并转换为字符串
(公共成员函数) [编辑]