命名空间
变体
操作

std::filesystem::recursive_directory_iterator

来自 cppreference.cn
 
 
 
 
定义于头文件 <filesystem>
class recursive_directory_iterator;
(C++17 起)

recursive_directory_iterator 是一个 LegacyInputIterator,它遍历目录的 directory_entry 元素,并递归遍历所有子目录的条目。迭代顺序未指定,除了每个目录条目只被访问一次。

默认情况下,不跟随符号链接,但可以通过在构造时指定目录选项 follow_directory_symlink 来启用此功能。

特殊路径名 dotdot-dot 被跳过。

如果 recursive_directory_iterator 报告错误或在遍历到顶级目录的最后一个目录条目之后前进,它将等同于默认构造的迭代器,也称为结束迭代器。两个结束迭代器始终相等,解引用或递增结束迭代器是未定义行为。

如果在创建递归目录迭代器之后从目录树中删除或添加了文件或目录,则迭代器是否会观察到更改是未指定的。

如果目录结构包含循环,则可能无法到达结束迭代器。

目录

[编辑] 成员类型

成员类型 定义
value_type std::filesystem::directory_entry
difference_type std::ptrdiff_t
pointer const std::filesystem::directory_entry*
reference const std::filesystem::directory_entry&
iterator_category std::input_iterator_tag

[编辑] 成员函数

构造一个递归目录迭代器
(public 成员函数) [编辑]
(析构函数)
默认析构函数
(public 成员函数) [编辑]
观察器
访问指向的条目
(public 成员函数) [编辑]
返回当前影响迭代的活动选项
(public 成员函数) [编辑]
返回当前递归深度
(public 成员函数) [编辑]
检查当前目录是否禁用递归
(public 成员函数) [编辑]
修改器
赋值内容
(public 成员函数) [编辑]
前进到下一个条目
(public 成员函数) [编辑]
在目录层次结构中向上移动迭代器一级
(public 成员函数) [编辑]
禁用递归直到下一次递增
(public 成员函数) [编辑]

[编辑] 非成员函数

支持基于范围的 for 循环
(函数) [编辑]

此外,operator==operator!=(直到 C++20)operator==(自 C++20 起) 按照 LegacyInputIterator 的要求提供。

是否提供 operator!=,因为它可以通过 operator== 合成,以及(自 C++20 起) 等式运算符是成员还是非成员是未指定的。

[编辑] 辅助特化

template<>

constexpr bool

    ranges::enable_borrowed_range<std::filesystem::recursive_directory_iterator> = true;
(C++20 起)
template<>

constexpr bool

    ranges::enable_view<std::filesystem::recursive_directory_iterator> = true;
(C++20 起)

这些针对 recursive_directory_iterator 的特化使其成为 borrowed_rangeview

[编辑] 注意

recursive_directory_iterator 通常持有一个引用计数的指针(以满足 LegacyInputIterator 的浅拷贝语义)指向一个实现对象,该对象包含:

[编辑] 示例

#include <filesystem>
#include <fstream>
#include <iostream>
#include <string>
namespace fs = std::filesystem;
 
int main()
{
    std::filesystem::current_path(std::filesystem::temp_directory_path());
    std::filesystem::create_directories("sandbox/a/b");
    std::ofstream("sandbox/file1.txt");
    std::filesystem::create_symlink("a", "sandbox/syma");
 
    // Iterate over the std::filesystem::directory_entry elements explicitly
    auto entry_length{3UZ};
    for (const fs::directory_entry& dir_entry :
            fs::recursive_directory_iterator("sandbox"))
    {
        std::cout << dir_entry << '\n';
        if (auto l{dir_entry.path().string().length()}; entry_length < l)
            entry_length = l;
    }
    std::cout << std::string(entry_length + 2, '-') << '\n';
 
    // Iterate over the std::filesystem::directory_entry elements using `auto`
    for (auto const& dir_entry : fs::recursive_directory_iterator("sandbox"))
        std::cout << dir_entry << '\n';
 
    std::filesystem::remove_all("sandbox");
}

可能的输出

"sandbox/syma"
"sandbox/file1.txt"
"sandbox/a"
"sandbox/a/b"
-------------------
"sandbox/syma"
"sandbox/file1.txt"
"sandbox/a"
"sandbox/a/b"

[编辑] 缺陷报告

下列更改行为的缺陷报告追溯地应用于以前出版的 C++ 标准。

缺陷报告 应用于 发布时的行为 正确的行为
LWG 3480 C++20 recursive_directory_iterator 既不是 borrowed_range 也不是 view 它两者都是

[编辑] 另请参阅

指向目录内容的迭代器
(类) [编辑]
目录项
(类) [编辑]
迭代目录内容的选项
(枚举) [编辑]