std::experi...ory_iterator(3)

πŸ“¦ version 2024.07.05, in nixpkgs unstable, opensuse-leap 16.0

πŸ’… 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_entry
  • difference_type β€” std::ptrdiff_t
  • pointer β€” 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 entry
  • operator-> β€” (public member function)
  • increment β€” advances to the next entry
  • operator++ β€” (public member function)

πŸ¦‹ Non-member functions πŸ¦‹

  • filesystem::begin(filesystem::directory_iterator) β€” range-based for loop support
  • filesystem::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