path クラス

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

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

構文

class path;

コンストラクター

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

Typedefs

型名 説明
const_iterator iterator と同義。
iterator mynamepath コンポーネントを指定する双方向の定数反復子。
string_type 型は、basic_string<value_type> の同意語。

メンバー関数

メンバー関数 説明
append 指定したシーケンスを mypath に追加し、必要に応じて変換し、preferred_separator を挿入します。
assign mypath を指定したシーケンスに置き換え、必要に応じて変換します。
begin パス名に含まれる最初のパス要素を指定している path::iterator を返します (存在する場合)。
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 mynamestem コンポーネントを返します。
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 を返します (存在する場合)。

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()) を返します。 2 番目の関数は mypath.compare(str) を返します。 3 番目の関数は、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;
   // ...
   };

解説

このクラスは、シーケンスに含まれる mynamepath コンポーネントを指定する、双方向の定数反復子を表します。

  1. ルート名 (存在する場合)

  2. ルート ディレクトリ (存在する場合)

  3. path の残りのディレクトリ要素 (存在する場合)。この要素の最後は、ファイル名になります (存在する場合)

pval の場合は、path 型のオブジェクトです。

  1. path::iterator X = pval.begin() は、パス名内の最初の path 要素を指定します (存在する場合)。

  2. X == pval.end() は、X がコンポーネント シーケンスの末尾の直後をポイントしたときに true になります。

  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.txtc:\\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

解説

1 つ目のメンバー演算子は、right.mynamemyname にコピーします。 2 つ目のメンバー演算子は right.mynamemyname に移動します。 3 番目のメンバー演算子は、*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_type または Elem

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 を構築します。

path() の場合は myname() です。

path(const path& right) の場合は myname(right.myname) です。

path(path&& right) の場合は myname(right.myname) です。

template<class Source> path(const Source& source) の場合は myname(source) です。

template<class Source> path(const Source& source, const locale& loc) の場合は myname(source) です。必要な codecvt ファセットは loc から取得します。

template<class InIt> path(InIt first, InIt last) の場合は myname(first, last) です。

template<class InIt> path(InIt first, InIt last, const locale& loc) の場合は myname(first, last) です。必要な codecvt ファセットは loc から取得します。

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())、dotmyname に追加されます。 次に、newextmyname に追加されます。

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

mynamestem コンポーネントを返します。

path stem() const;

解説

mynamestem コンポーネント (具体的には、後続の extension().native() を削除した filename().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;

解説

最初の (テンプレート) メンバー関数は、mypath に格納されているシーケンスを次と同じ方法で変換します。

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

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

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

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

2 番目のメンバー関数は、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;

関連項目

ヘッダー ファイル リファレンス