Namespaces
Variants

std::filesystem:: recursive_directory_iterator

From cppreference.net
헤더 파일에 정의됨 <filesystem>
class recursive_directory_iterator ;
(C++17부터)

recursive_directory_iterator 는 디렉토리의 LegacyInputIterator 로, 디렉토리의 directory_entry 요소들을 순회하고, 재귀적으로 모든 하위 디렉토리의 항목들을 순회합니다. 반복 순서는 명시되지 않으나, 각 디렉토리 항목은 한 번만 방문됩니다.

기본적으로 심볼릭 링크는 따르지 않지만, 생성 시 디렉토리 옵션 follow_directory_symlink 을 지정하면 활성화할 수 있습니다.

특수 경로명 dot dot-dot 은 건너뜁니다.

만약 recursive_directory_iterator 가 오류를 보고하거나 최상위 디렉토리의 마지막 디렉토리 엔트리를 지나서 진행되면, 기본 생성된 반복자(종료 반복자라고도 함)와 동일하게 됩니다. 두 개의 종료 반복자는 항상 동일하며, 종료 반복자를 역참조하거나 증가시키는 것은 정의되지 않은 동작입니다.

디렉토리 트리에 파일이나 디렉토리가 추가되거나 삭제된 후 재귀적 디렉토리 반복자가 생성된 경우, 해당 변경 사항이 반복자를 통해 관찰될지 여부는 명시되어 있지 않습니다.

디렉토리 구조에 순환(cycle)이 포함된 경우, end 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

멤버 함수

재귀 디렉터리 반복자를 생성합니다
(public member function)
(destructor)
기본 소멸자
(public member function)
Observers
가리키는 항목에 접근합니다
(public member function)
현재 반복에 영향을 주는 활성 옵션을 반환합니다
(public member function)
현재 재귀 깊이를 반환합니다
(public member function)
현재 디렉터리에 대해 재귀가 비활성화되었는지 확인합니다
(public member function)
Modifiers
내용을 할당합니다
(public member function)
다음 항목으로 진행합니다
(public member function)
반복자를 디렉터리 계층 구조에서 한 단계 위로 이동합니다
(public member function)
다음 증가까지 재귀를 비활성화합니다
(public member function)

비멤버 함수

범위 기반 for 루프 지원
(함수)

또한, operator== operator!= (C++20까지) operator== (C++20부터) LegacyInputIterator 요구 사항에 따라 제공됩니다.

operator== 에서 합성될 수 있기 때문에 operator!= 가 제공되는지 여부는 명시되지 않으며, (C++20부터) 동등성 연산자가 멤버인지 비멤버인지 여부도 명시되지 않습니다.

헬퍼 특수화

template <>

constexpr bool

ranges:: enable_borrowed_range < std :: filesystem :: recursive_directory_iterator > = true ;
(C++20부터)
template <>

constexpr bool

ranges:: enable_view < std :: filesystem :: recursive_directory_iterator > = true ;
(C++20부터)

이러한 recursive_directory_iterator 에 대한 특수화는 이를 borrowed_range view 로 만듭니다.

참고 사항

recursive_directory_iterator 는 일반적으로 참조 카운팅된 포인터 ( LegacyInputIterator 의 얕은 복사 의미론을 충족시키기 위해)를 보유하며, 이는 다음을 포함하는 구현 객체를 가리킵니다:

예제

#include <filesystem>
#include <fstream>
#include <iostream>
#include <string>
namespace fs = std::filesystem;
int main()
{
    std::filesystem::current_path(std::filesystem::temp_directory_path());
    std::filesystem::create_directories("sandbox/a/b");
    std::ofstream("sandbox/file1.txt");
    std::filesystem::create_symlink("a", "sandbox/syma");
    // std::filesystem::directory_entry 요소들을 명시적으로 반복
    auto entry_length{3UZ};
    for (const fs::directory_entry& dir_entry :
            fs::recursive_directory_iterator("sandbox"))
    {
        std::cout << dir_entry << '\n';
        if (auto l{dir_entry.path().string().length()}; entry_length < l)
            entry_length = l;
    }
    std::cout << std::string(entry_length + 2, '-') << '\n';
    // `auto`를 사용하여 std::filesystem::directory_entry 요소들 반복
    for (auto const& dir_entry : fs::recursive_directory_iterator("sandbox"))
        std::cout << dir_entry << '\n';
    std::filesystem::remove_all("sandbox");
}

가능한 출력:

"sandbox/syma"
"sandbox/file1.txt"
"sandbox/a"
"sandbox/a/b"
-------------------
"sandbox/syma"
"sandbox/file1.txt"
"sandbox/a"
"sandbox/a/b"

결함 보고서

다음의 동작 변경 결함 보고서들은 이전에 발표된 C++ 표준에 소급 적용되었습니다.

DR 적용 대상 게시된 동작 올바른 동작
LWG 3480 C++20 recursive_directory_iterator borrowed_range 도 아니고 view 도 아니었음 둘 다 해당됨

참고 항목

디렉토리 내용에 대한 반복자
(클래스)
디렉토리 엔트리
(클래스)
디렉토리 내용 반복을 위한 옵션들
(열거형)