命名空间
变体
操作

std::filesystem::directory_iterator

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

directory_iterator 是一个 LegacyInputIterator,它迭代目录的 directory_entry 元素(但不访问子目录)。迭代顺序是未指定的,除了每个目录条目只访问一次。特殊的路径名 dotdot-dot 会被跳过。

如果 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

[编辑] 成员函数

构造一个目录迭代器
(公共成员函数) [编辑]
(析构函数)
默认析构函数
(公共成员函数) [编辑]
赋值内容
(公共成员函数) [编辑]
访问指向的条目
(公共成员函数) [编辑]
前进到下一个条目
(公共成员函数) [编辑]

[编辑] 非成员函数

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

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

是否提供 operator!= 是未指定的,因为它可以通过 operator== 合成,并且(始于 C++20) 相等运算符是成员还是非成员也是未指定的。

[编辑] 辅助特化

template<>

constexpr bool

    ranges::enable_borrowed_range<std::filesystem::directory_iterator> = true;
(始于 C++20)
template<>

constexpr bool

    ranges::enable_view<std::filesystem::directory_iterator> = true;
(始于 C++20)

这些对于 directory_iterator 的特化使其成为 borrowed_rangeview

[编辑] 注释

许多用于目录遍历的底层操作系统 API 在检索下一个目录条目的同时检索文件属性。 std::filesystem::directory_iterator 的构造函数和非常量成员函数将这些属性(如果有)存储在指向的 std::filesystem::directory_entry 中,而无需调用 directory_entry::refresh,这使得检查正在迭代的目录条目的属性成为可能,而无需进行额外的系统调用。

[编辑] 示例

#include <algorithm>
#include <filesystem>
#include <fstream>
#include <iostream>
 
int main()
{
    const std::filesystem::path sandbox{"sandbox"};
    std::filesystem::create_directories(sandbox/"dir1"/"dir2");
    std::ofstream{sandbox/"file1.txt"};
    std::ofstream{sandbox/"file2.txt"};
 
    std::cout << "directory_iterator:\n";
    // directory_iterator can be iterated using a range-for loop
    for (auto const& dir_entry : std::filesystem::directory_iterator{sandbox}) 
        std::cout << dir_entry.path() << '\n';
 
    std::cout << "\ndirectory_iterator as a range:\n";
    // directory_iterator behaves as a range in other ways, too
    std::ranges::for_each(
        std::filesystem::directory_iterator{sandbox},
        [](const auto& dir_entry) { std::cout << dir_entry << '\n'; });
 
    std::cout << "\nrecursive_directory_iterator:\n";
    for (auto const& dir_entry : std::filesystem::recursive_directory_iterator{sandbox}) 
        std::cout << dir_entry << '\n';
 
    // delete the sandbox dir and all contents within it, including subdirs
    std::filesystem::remove_all(sandbox);
}

可能的输出

directory_iterator:
"sandbox/file2.txt"
"sandbox/file1.txt"
"sandbox/dir1"
 
directory_iterator as a range:
"sandbox/file2.txt"
"sandbox/file1.txt"
"sandbox/dir1"
 
recursive_directory_iterator:
"sandbox/file2.txt"
"sandbox/file1.txt"
"sandbox/dir1"
"sandbox/dir1/dir2"

[编辑] 缺陷报告

以下行为变更缺陷报告被追溯应用于先前发布的 C++ 标准。

DR 应用于 已发布行为 正确行为
LWG 3480 C++20 directory_iterator 既不是 borrowed_range 也不是 view 均为是

[编辑] 参见

指向目录及其子目录内容的迭代器
(类) [编辑]
迭代目录内容的选项
(枚举) [编辑]
一个目录条目
(类) [编辑]