c++ - std(boost)::filesystem::path 组件数

标签 c++ c++17 boost-filesystem

有没有什么惯用的方法可以使用文件系统库获取路径中的组件数量?还是我错过了一些方法?

或者我是否必须调用 parent_path() 直到到达根目录?

最佳答案

size() 方法怎么样?

boost::filesystem::path p;
// fill p
std::cout << p.size() << std::endl;

会给你组件的数量。

此外,path 迭代器不会遍历路径的字符串,而是遍历路径的组成部分。所以这也应该有效:

std::distance(p.begin(), p.end());

关于c++ - std(boost)::filesystem::path 组件数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45762968/

相关文章:

c++ - 视觉 C : Namespace Scope Bug or Obscure Feature?

c++ - 如何断言 constexpr if else 子句永远不会发生?

boost - boost directory_iterator示例-如何列出不递归的目录文件

c++ - 当 QMainWindow 调整大小时,QDockWidget 分割器会跳转

c++ - 在范围之间创建一个随机偶数

c++ - 如何在 C++ 中制作管道

boost - 为什么 Boost::Filesystem 有二进制部分?

c++ - qtreewidgetitem addchild 到已经存在的项目不会产生任何结果

c++ - 模板中的可选变量声明

c++ - 为什么在 boost 程序 (boost::filesystem) 中输出 "Bus error: 10"?