std::experi...ator::depth(3)
π 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
- 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