std::experi...ory_iterator(3)
π NAME π
std::experimental::filesystem::recursive_directory_iterator β std::experimental::filesystem::recursive_directory_iterator
okay so this page is from the C++ Standard Libary manual (section 3, and yes bestie, βLibaryβ is literally their spelling, not a typo I made ππ). Sheβs a whole recursive directory iterator and she is main character energy, periodt.
β¨ Synopsis β¨
Defined in header <experimental/filesystem>:
class recursive_directory_iterator; (filesystem TS)
recursive_directory_iterator is a LegacyInputIterator that iterates over the directory_entry elements of a directory, and, recursively, over the entries of all subdirectories. The iteration order is unspecified, except that each directory entry is visited only once. (so like, donβt expect alphabetical β sheβs mysterious π
)
By default, symlinks are not followed, but this can be enabled by specifying the directory option follow_directory_symlink at construction time.
The special pathnames dot and dot-dot are skipped. we love a self-care queen who doesnβt loop on herself π«Ά
If the recursive_directory_iterator is advanced past the last directory entry of the top-level directory, it becomes equal to the default-constructed iterator, also known as the end iterator. Two end iterators are always equal, dereferencing or incrementing the end iterator is undefined behavior. like, donβt do it, fr fr π¨
If a file or a directory is deleted or added to the directory tree after the recursive directory iterator has been created, it is unspecified whether the change would be observed through the iterator.
β οΈ If the directory structure contains cycles, the end iterator may be unreachable. Thatβs not a vibe, bestie β plan accordingly π.
πΈ Member types πΈ
value_typeβfilesystem::directory_entrydifference_typeβstd::ptrdiff_tpointerβconst filesystem::directory_entry*referenceβconst filesystem::directory_entry&iterator_categoryβstd::input_iterator_tag
π¦ Member functions π¦
constructorβ constructs a recursive directory iterator (public member function).destructorβ default destructor (public member function).
π Observers π
operator*β accesses the pointed-to entry.operator->β (public member function).optionsβ returns the currently active options that affect the iteration (public member function).depthβ returns the current recursion depth (public member function).recursion_pendingβ checks whether the recursion is disabled for the current directory (public member function).
π₯ Modifiers π₯
operator=β assigns contents (public member function).incrementβ advances to the next entry.operator++β (public member function).popβ moves the iterator one level up in the directory hierarchy (public member function).disable_recursion_pendingβ disables recursion until the next increment (public member function).
π Non-member functions π
filesystem::begin(filesystem::recursive_directory_iterator)β range-based for loop support (function).filesystem::end(filesystem::recursive_directory_iterator)β range-based for loop support (function).
Additionally, operator== and operator!= are provided, either as members or as non-members, as required by LegacyInputIterator. equality said βitβs giving input iterator requirementsβ π
β Notes β
A recursive_directory_iterator typically holds a reference-counted pointer (to satisfy shallow-copy semantics of LegacyInputIterator) to an implementation object, which holds:
- a container (such as
std::vector) of non-recursivedirectory_iteratorsthat forms the recursion stack. - the recursion depth counter (accessible with
depth()). - the directory options used at construction (accessible with
options()). - the pending recursion flag (accessible with
recursion_pending(), may be combined with the directory options to save space).
not me being obsessed with implementation details πβ¨
π Example π
// Run this code
#include <experimental/filesystem>
#include <fstream>
#include <iostream>
namespace fs = std::experimental::filesystem;
int main()
{
fs::create_directories("sandbox/a/b");
std::ofstream("sandbox/file1.txt");
fs::create_symlink("a", "sandbox/syma");
for (const fs::directory_entry& entry : fs::recursive_directory_iterator("sandbox"))
std::cout << entry << '\n';
fs::remove_all("sandbox");
}
π Possible output π
"sandbox/a"
"sandbox/a/b"
"sandbox/file1.txt"
"sandbox/syma"
π See also π
directory_iteratorβ an iterator to the contents of the directory (class).directory_entryβ a directory entry (class).directory_optionsβ options for iterating directory contents (enum).
π§ Category π§
- Noindexed pages
k thatβs the whole page, bestie π source/version is http://cppreference.com, dated 2024.06.10, and the footer title is std::experi...ory_iterator(3). mwah πβ¨
π¦ where this page comes from
- nixpkgs unstable: stdman 2024.07.05
- opensuse-leap 16.0: stdman 2024.07.05-bp160.1.9 Β· MIT
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms