std::filesystem::recursive_directory_iterator
定義於標頭檔案 <filesystem> |
||
class recursive_directory_iterator; |
(C++17 起) | |
recursive_directory_iterator
是一個 LegacyInputIterator,它遍歷目錄的 directory_entry 元素,並遞迴遍歷所有子目錄的條目。迭代順序未指定,除了每個目錄條目只被訪問一次。
預設情況下,不跟隨符號連結,但可以透過在構造時指定目錄選項 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 |
[編輯] 成員函式
構造一個遞迴目錄迭代器 (public 成員函式) | |
(解構函式) |
預設解構函式 (public 成員函式) |
觀察器 | |
訪問指向的條目 (public 成員函式) | |
返回當前影響迭代的活動選項 (public 成員函式) | |
返回當前遞迴深度 (public 成員函式) | |
檢查當前目錄是否停用遞迴 (public 成員函式) | |
修改器 | |
賦值內容 (public 成員函式) | |
前進到下一個條目 (public 成員函式) | |
在目錄層次結構中向上移動迭代器一級 (public 成員函式) | |
停用遞迴直到下一次遞增 (public 成員函式) |
[編輯] 非成員函式
支援基於範圍的 for 迴圈 (函式) |
此外,operator==
和 operator!=
(直到 C++20)operator==
(自 C++20 起) 按照 LegacyInputIterator 的要求提供。
是否提供 operator!=
,因為它可以透過 operator==
合成,以及(自 C++20 起) 等式運算子是成員還是非成員是未指定的。
[編輯] 輔助特化
template<> constexpr bool |
(C++20 起) | |
template<> constexpr bool |
(C++20 起) | |
這些針對 recursive_directory_iterator
的特化使其成為 borrowed_range
和 view
。
[編輯] 注意
recursive_directory_iterator
通常持有一個引用計數的指標(以滿足 LegacyInputIterator 的淺複製語義)指向一個實現物件,該物件包含:
- 一個非遞迴 directory_iterators 容器(如 std::vector),形成遞迴棧,
- 遞迴深度計數器(可透過 depth() 訪問),
- 構造時使用的目錄選項(可透過 options() 訪問),
- 待定遞迴標誌(可透過 recursion_pending() 訪問,可與目錄選項結合以節省空間)。
[編輯] 示例
#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"); // Iterate over the std::filesystem::directory_entry elements explicitly 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'; // Iterate over the std::filesystem::directory_entry elements using `auto` 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++ 標準。
缺陷報告 | 應用於 | 釋出時的行為 | 正確的行為 |
---|---|---|---|
LWG 3480 | C++20 | recursive_directory_iterator 既不是 borrowed_range 也不是 view |
它兩者都是 |
[編輯] 另請參閱
(C++17) |
指向目錄內容的迭代器 (類) |
(C++17) |
目錄項 (類) |
(C++17) |
迭代目錄內容的選項 (列舉) |