std::filesystem::recursive_directory_iterator

std::filesystem::recursive_directory_iterator (재귀 디렉터리 순회 반복자)

디렉터리의 directory_entry 요소들을, 그리고 모든 하위 디렉터리의 항목들을 재귀적으로 순회하는 LegacyInputIterator예요. C++17부터 있어요.

출처: cppreference

본문

<filesystem> 헤더에 정의돼 있어요.

class recursive_directory_iterator;

recursive_directory_iterator는 디렉터리의 directory_entry 요소들을, 그리고 모든 하위 디렉터리의 항목들을 재귀적으로 순회하는 LegacyInputIterator예요. 순회 순서는 명세되지 않지만, 각 디렉터리 항목은 정확히 한 번 방문돼요.

기본적으로 심볼릭 링크는 따라가지 않지만, 생성 시 directory_options::follow_directory_symlink 옵션을 지정해 활성화할 수 있어요. 특수 경로 이름 .(dot)..(dot-dot)는 건너뛰어져요.

recursive_directory_iterator가 오류를 보고하거나 최상위 디렉터리의 마지막 디렉터리 항목을 지나치면 기본 생성된 반복자(= 끝 반복자)와 같아져요. 두 끝 반복자는 항상 같고, 끝 반복자를 역참조·증가하는 것은 정의되지 않은 동작이에요. 생성 후 디렉터리 트리에 변경이 생기면 그 변경이 관찰될지 여부는 명세되지 않아요. 디렉터리 구조에 순환이 있으면 끝 반복자에 도달하지 못할 수 있어요.

멤버 타입

멤버 타입 정의
value_type std::filesystem::directory_entry
difference_type std::ptrdiff_t
pointer const std::filesystem::directory_entry*
reference const std::filesystem::directory_entry&
iterator_category std::input_iterator_tag

멤버 함수

  • (constructor) — 재귀 디렉터리 반복자를 생성해요.
  • (destructor) — 기본 소멸자.
  • operator, operator->* — 가리키는 항목에 접근해요.
  • options — 순회에 현재 영향을 주는 옵션을 돌려줘요.
  • depth — 현재 깊이(최상위 디렉터리에서 현재 디렉터리까지의 거리)를 돌려줘요.
  • recursion_pending — 재귀가 보류 중인지 돌려줘요.
  • disable_recursion_pending — 재귀를 비활성화해요.
  • increment, operator++ — 다음 항목으로 나아가요.
  • pop — 디렉터리에서 위로 이동해요.

비멤버 함수

  • begin, end (C++17) — 범위 기반 for 루프 지원.

예제

#include <filesystem>
#include <iostream>
namespace fs = std::filesystem;

int main()
{
    for (const auto& entry : fs::recursive_directory_iterator("."))
        std::cout << entry.path() << '\n';
}

더 알아보기 (Learn more)

cppreference