path クラス

path クラスでは、string_type 型のオブジェクト (ここでは、説明のために myname と呼びます) を格納し、パス名としての使用に適しています。 string_type は basic_string<value_type> のシノニムです。この場合、value_type は wchar_t (Windows 上) または char (POSIX 上) のシノニムです。

ここで説明する path クラスは、<filesystem> ヘッダーの C++17 std::filesystem::path クラスです。

詳細およびコード例については、ファイル システムのナビゲーション (C++) に関する記事を参照してください。

構文

class path;

コンストラクター

コンストラクター 説明
path path を構築します。

Typedefs

型名 説明
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 ネイティブ パス名形式でシーケンスを解釈します。
generic_format 汎用パス名形式でシーケンスを解釈します。

解説

ネイティブ形式とジェネリック形式はWindowsで交換可能であるため、Microsoft実装では 3 つの列挙子すべてが同じように扱われます。 標準準拠と移植性のために、 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() は、パス名内の最初の 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で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

構文 (文字列) 分析のみを使用して、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。

解説

1 つ目のメンバー演算子は、right.myname を myname にコピーします。 2 つ目のメンバー演算子は right.myname を myname に移動します。 3 番目のメンバー演算子は、 source を mynameに移動します。 4 番目のメンバー演算子は、 *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オーバーロードは、1 つの文字を連結します。 テンプレート化された 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
パス名formatsourceまたはfirst/lastの範囲を解釈するために使用されます。 既定値は 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 から取得します。

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 から取得します。

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())、dot が 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 に格納されているシーケンスを次と同じ方法で変換します。

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

2 番目のメンバー関数は、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>
ヘッダー ファイル リファレンス