std::filesystem::recursive_directory_iterator:: depth
From cppreference.net
<
cpp
|
filesystem
|
recursive directory iterator
C++
std::filesystem::recursive_directory_iterator
| Member functions | ||||
|
recursive_directory_iterator::depth
|
||||
| Non-member functions | ||||
|
int
depth
(
)
const
;
|
(C++17부터) | |
시작 디렉토리부터 현재 반복 중인 디렉토리까지의 디렉토리 개수를 반환합니다. 즉, 디렉토리 계층 구조의 현재 깊이를 나타냅니다.
시작 디렉토리의 깊이는 0 이며, 그 하위 디렉토리들의 깊이는 1 입니다.
* this 가 end iterator인 경우 동작은 정의되지 않습니다.
목차 |
매개변수
(없음)
반환값
현재 디렉토리 계층 구조의 깊이입니다.
예외
아무것도 던지지 않습니다.
예제
이 예제는 반복 깊이를 사용하여 디렉토리 트리 출력의 들여쓰기를 계산합니다.
이 코드 실행
#include <filesystem> #include <fstream> #include <iostream> #include <string> namespace fs = std::filesystem; int main() { fs::current_path(fs::temp_directory_path()); 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() << 1, ' ') << *i; if (fs::is_symlink(i->symlink_status())) std::cout << " -> " << fs::read_symlink(*i); std::cout << '\n'; } fs::remove_all("sandbox"); }
출력:
"sandbox/syma" -> "a"
"sandbox/a"
"sandbox/a/b"
"sandbox/a/b/d"
"sandbox/a/b/d/e"
"sandbox/a/b/file1.txt"
"sandbox/a/b/c"