• Class / Function / Type

      std::
    • Header file

      <>
    • Other / All

    最終更新日時(UTC):
    が更新

    履歴 編集

    function
    <filesystem>

    std::filesystem::path::parent_path

    path parent_path() const;
    

    概要

    親のパスを取得する。

    パスにルートパスのみが含まれている場合は、ルートパスがそのまま返る。ルートパスの親は自身のパスであると見なされる。

    戻り値

    !has_relative_path()であれば*thisが返る。そうでなければ、汎用フォーマットのパスに対して、ディレクトリ区切りした要素のうち、末尾要素を除いたパスを返す。

    POSIXベースシステムでの例

    #include <iostream>
    #include <filesystem>
    
    namespace fs = std::filesystem;
    
    int main()
    {
      fs::path ps[] = {
        "/foo/bar.txt", // ファイル名を含むパス
        "/foo/bar/",    // ディレクトリパス
        "/foo/bar",     // ディレクトリパス(末尾/なし)
        "/"             // ルートパスのみ (ルートパスの親はルートパスなのでそのまま返る)
      };
    
      for (const fs::path& p : ps) {
        std::cout << p << " : " << p.parent_path() << std::endl;
      }
    }
    

    出力

    "/foo/bar.txt" : "/foo"
    "/foo/bar/" : "/foo/bar"
    "/foo/bar" : "/foo"
    "/" : "/"
    

    Windowsでの例

    #include <iostream>
    #include <filesystem>
    
    namespace fs = std::filesystem;
    
    int main()
    {
      fs::path ps[] = {
        "C:/foo/bar.txt", // ファイル名を含むパス
        "C:/foo/bar/",    // ディレクトリパス
        "C:/"             // ルートパスのみ (ルートパスの親はルートパスなのでそのまま返る)
      };
    
      for (const fs::path& p : ps) {
        std::cout << p << " : " << p.parent_path() << std::endl;
      }
    }
    

    出力

    "C:/foo/bar.txt" : "C:/foo"
    "C:/foo/bar/" : "C:/foo/bar"
    "C:/" : "C:/"
    

    バージョン

    言語

    • C++17

    処理系

    参照