在线文档教程
C++
文件系统 | Filesystem

std::filesystem::directory_iterator

文件系统::目录[医]迭代器

Defined in header
class directory_iterator;(since C++17)

directory_iteratorInputIterator,则遍历directory_entry目录%28的元素,但不访问子目录%29。迭代顺序未指定,只是每个目录条目只访问一次。跳过特殊路径名点和点点.

如果directory_iterator在最后一个目录条目之后,它就等于默认构造的迭代器(也称为结束迭代器)。两个结束迭代器总是相等的,取消引用或递增结束迭代器是未定义的行为。

如果在创建目录迭代器后删除或添加文件或目录树,则未指定是否通过迭代器观察更改。

成员类型

Member typeDefinition
value_typestd::filesystem::directory_entry
difference_typestd::ptrdiff_t
pointerconst std::filesystem::directory_entry*
referenceconst std::filesystem::directory_entry&
iterator_categorystd::input_iterator_tag

成员函数

(constructor)constructs a directory iterator (public member function)
(destructor)default destructor (public member function)
operator=assigns contents (public member function)
operator*operator->accesses the pointed-to entry (public member function)
incrementoperator++advances to the next entry (public member function)

非会员职能

begin(std::filesystem::directory_iterator)end(std::filesystem::directory_iterator)range-based for loop support (function)

此外,operator==operator!=提供,或作为成员或非会员,按InputIterator...

二次

#include <fstream> #include <iostream> #include <filesystem> namespace fs = std::filesystem; int main() { fs::create_directories("sandbox/a/b" std::ofstream("sandbox/file1.txt" std::ofstream("sandbox/file2.txt" for(auto& p: fs::directory_iterator("sandbox")) std::cout << p << '\n'; fs::remove_all("sandbox" }

二次

可能的产出:

二次

"sandbox/a" "sandbox/file1.txt" "sandbox/file2.txt"

二次

另见

recursive_directory_iterator (C++17)an iterator to the contents of a directory and its subdirectories (class)
directory_options (C++17)options for iterating directory contents (enum)
directory_entry (C++17)a directory entry (class)

© cppreference.com

在CreativeCommonsAttribution下授权-ShareAlike未移植许可v3.0。

http://en.cppremience.com/w/cpp/filesystem/目录[医]迭代器