Namespaces
Variants

std::filesystem::path:: begin, std::filesystem::path:: end

From cppreference.net
iterator begin ( ) const ;
(1) (自 C++17 起)
iterator end ( ) const ;
(2) (自 C++17 起)
1) 返回指向路径第一个元素的迭代器。如果路径为空,返回的迭代器等于 end()
2) 返回指向路径最后一个元素之后位置的迭代器。解引用此迭代器将导致未定义行为。

该迭代器对所表示的序列包含以下内容:

  1. 根名称 (如果存在)。
  2. 根目录 (如果存在)。
  3. 一系列 文件名 ,省略所有目录分隔符。
  4. 如果路径中最后一个 文件名 后面有目录分隔符,则结束迭代器之前的最后一个元素为空元素。

目录

参数

(无)

返回值

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" │