std::filesystem::path::begin, std::filesystem::path::end
来自 cppreference.cn
< cpp | filesystem | path
iterator begin() const; |
(1) | (C++17 起) |
iterator end() const; |
(2) | (C++17 起) |
1) 返回指向路径首个元素的迭代器。若路径为空,则返回的迭代器等于 end()。
2) 返回指向路径最后一个元素之后一位的迭代器。解引用此迭代器是未定义行为。
这对迭代器所表示的序列由以下部分组成
- 根名(root-name)(若存在)。
- 根目录(root-directory)(若存在)。
- 一系列文件名(file-name),省略任何目录分隔符。
- 如果路径中最后一个文件名(file-name)之后有一个目录分隔符,则尾后迭代器之前的最后一个元素是一个空元素。
目录 |
[编辑] 参数
(无)
[编辑] 返回值
1) 指向路径首个元素的迭代器。
2) 指向路径末尾之后一位的迭代器
[编辑] 异常
可能抛出实现定义的异常。
[编辑] 示例
运行此代码
#include <filesystem> #include <iostream> namespace fs = std::filesystem; int main() { const fs::path p = # ifdef _WIN32 "C:\\users\\abcdef\\AppData\\Local\\Temp\\"; # else "/home/user/.config/Cppcheck/Cppcheck-GUI.conf"; # endif std::cout << "Examining the path " << p << " through iterators gives\n"; for (auto it = p.begin(); it != p.end(); ++it) std::cout << *it << " │ "; std::cout << '\n'; }
可能的输出
--- Windows --- Examining the path "C:\users\abcdef\AppData\Local\Temp\" through iterators gives "C:" │ "/" │ "users" │ "abcdef" │ "AppData" │ "Local" │ "Temp" │ "" │ --- UNIX --- Examining the path "/home/user/.config/Cppcheck/Cppcheck-GUI.conf" through iterators gives "/" │ "home" │ "user" │ ".config" │ "Cppcheck" │ "Cppcheck-GUI.conf" │