std::experi...ator::depth(3)

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

πŸ’… std::experi...ator::depth(3) πŸ’…

hiii bestie, welcome to this page from the C++ Standard Libary manual (yes, β€œLibary” with one b, that's how it's spelled here, periodt). It's all about std::experimental::filesystem::recursive_directory_iterator::depth, and it's lowkey iconic for making directory trees look cute.


🌸 NAME 🌸

okay so the name is std::experimental::filesystem::recursive_directory_iterator::depth. And the original page is extra and lists it twice, so here it is again: std::experimental::filesystem::recursive_directory_iterator::depth. Same name, twice, we love consistency. 🫢


πŸŒ€ Synopsis πŸŒ€

here's the signature, and yes it's from the filesystem TS:

int depth() const;  (filesystem TS)

So what does it do? It returns the number of directories from the starting directory to the currently iterated directory, i.e. the current depth of the directory hierarchy.

The starting directory has depth of 0, its subdirectories have depth 1, etc. Cute, right? πŸŽ€

⚠️ The behavior is undefined if *this is the end iterator. Like, do not call it there, bestie β€” undefined behavior is not a vibe. πŸ’€

🧚 Parameters 🧚

(none) β€” literally no parameters, not a single one. It's giving minimalist main character energy. ✨


πŸŽ€ Return value πŸŽ€

Current depth of the directory hierarchy. That's it. That's the whole flex. πŸ’–


🚨 Exceptions 🚨

Throws nothing. Not a single exception. Chillest function in the filesystem TS, ngl. 😌


πŸ’» Example πŸ’»

This example uses iteration depth to calculate the indentation of a directory tree printout. The way i.depth() decides how many spaces go before each line? Obsessed. πŸ¦‹

// Run this code

#include <experimental/filesystem>
#include <fstream>
#include <iostream>
#include <string>
namespace fs = std::experimental::filesystem;

int main()
{
    fs::create_directories("sandbox/a/b/c");
    fs::create_directories("sandbox/a/b/d/e");
    std::ofstream("sandbox/a/b/file1.txt");
    fs::create_symlink("a", "sandbox/syma");
    for (auto i = fs::recursive_directory_iterator("sandbox");
             i != fs::recursive_directory_iterator(); ++i)
    {
        std::cout << std::string(i.depth(), ' ') << *i;
        if (fs::is_symlink(i->symlink_status()))
            std::cout << " -> " << fs::read_symlink(*i);
        std::cout << '\n';
    }
    fs::remove_all("sandbox");
}

πŸ“œ Output πŸ“œ

the printout is:

"sandbox/a"
 "sandbox/a/b"
  "sandbox/a/b/c"
  "sandbox/a/b/d"
   "sandbox/a/b/d/e"
  "sandbox/a/b/file1.txt"
"sandbox/syma" -> "a"

look at those little indents doing their thing. slay. πŸ’…


🏷️ Category 🏷️

  • * Noindexed pages

mwah πŸ’‹ β€” this cutie comes from source/version http://cppreference.com, date 2024.06.10, page std::experi...ator::depth(3). bye bestie! πŸ¦‹

πŸ“¦ where this page comes from