命名空间
变体
操作

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

[编辑] 成员函数

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

[编辑] 非成员函数

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

此外,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

[编辑] 注意

许多用于目录遍历的低级 OS API 在检索下一个目录条目的同时检索文件属性。std::filesystem::directory_iterator 的构造函数和非 const 成员函数将这些属性(如果有)存储在指向的 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++ 标准。

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

[编辑] 另见

指向目录及其子目录内容的迭代器
(class) [编辑]
迭代目录内容的选项
(enum) [编辑]
目录项
(class) [编辑]