std::experi...ory_iterator(3)
π std::experimental::filesystem::directory_iterator
okay bestie, welcome to the C++ Standard Library manual page for std::experimental::filesystem::directory_iterator β yes, the whole iconic mouthful. Itβs giving βfilesystem but experimental,β and weβre gonna get through it together. π«Ά
πΈ NAME πΈ
std::experimental::filesystem::directory_iterator β literally that. Thatβs the name. No cap. π
β¨ Synopsis β¨
Defined in header <experimental/filesystem>:
class directory_iterator; (filesystem TS)
directory_iterator is a LegacyInputIterator that iterates over the directory_entry elements of a directory β but bestie, it does not visit the subdirectories. The iteration order is unspecified, except that each directory entry is visited only once. The special pathnames dot and dot-dot are skipped. π
If the directory_iterator is advanced past the last directory entry, 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, do not do that, fr fr. π«
β οΈ If a file or a directory is deleted or added to the directory tree after the directory iterator has been created, it is unspecified whether the change would be observed through the iterator. So yeah, the vibes are βunspecified,β not βguaranteed.β π
π 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 directory iterator (public member function)destructorβ default destructor (public member function)operator=β assigns contents (public member function)operator*β accesses the pointed-to entryoperator->β (public member function)incrementβ advances to the next entryoperator++β (public member function)
π¦ Non-member functions π¦
filesystem::begin(filesystem::directory_iterator)β range-basedforloop supportfilesystem::end(filesystem::directory_iterator)β (function)
Additionally, operator== and operator!= are provided, either as members or as non-members, as required by LegacyInputIterator. periodt. π
π 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"};
std::ofstream{"sandbox/file2.txt"};
for (const fs::directory_entry& entry : fs::directory_iterator{"sandbox"})
std::cout << entry << '\n';
fs::remove_all("sandbox");
}
π Possible output π
"sandbox/a"
"sandbox/file1.txt"
"sandbox/file2.txt"
π See also π
recursive_directory_iteratorβ an iterator to the contents of a directory and its subdirectories (class)directory_optionsβ options for iterating directory contents (enum)directory_entryβ a directory entry (class)
π± Category π±
- Noindexed pages
mwah π thatβs the whole page. Footer vibe check: source/version is http://cppreference.com, and the date is 2024.06.10. slay. π«Άβ¨
π¦ 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