命名空间
变体
操作

std::filesystem::path::parent_path

来自 cppreference.cn
< cpp‎ | filesystem‎ | path
 
 
 
 
path parent_path() const;
(C++17 起)

返回到父目录的路径。

has_relative_path() 返回 false ,则结果为 *this 的副本。否则,结果是其泛型格式路径名为 *this 的泛型格式路径名的最长前缀,并且其迭代中少产生一个元素的路径。

目录

[编辑] 参数

(无)

[编辑] 返回值

到父目录的路径,或若非 has_relative_path() 则为 *this 的副本。

[编辑] 异常

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

[编辑] 示例

#include <filesystem>
#include <iostream>
namespace fs = std::filesystem;
 
int main()
{
    for (fs::path p : {"/var/tmp/example.txt", "/", "/var/tmp/."})
        std::cout << "The parent path of " << p
                  << " is " << p.parent_path() << '\n';
}

可能的输出

The parent path of "/var/tmp/example.txt" is "/var/tmp"
The parent path of "/" is "/"
The parent path of "/var/tmp/." is "/var/tmp"

[编辑] 参阅

返回路径的根名(如果存在)
(公开成员函数) [编辑]
返回路径的根目录(如果存在)
(公开成员函数) [编辑]
返回路径的根路径(如果存在)
(公开成员函数) [编辑]