名稱空間
變體
操作

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

來自 cppreference.com
< cpp‎ | filesystem‎ | path
 
 
 
 
iterator begin() const;
(1) (C++17 起)
iterator end() const;
(2) (C++17 起)
1) 返回指向路徑首個元素的迭代器。若路徑為空,則返回的迭代器等於 end()
2) 返回指向路徑最後一個元素之後一位的迭代器。解引用此迭代器是未定義行為。

這對迭代器所表示的序列由以下部分組成

  1. 根名(root-name)(若存在)。
  2. 根目錄(root-directory)(若存在)。
  3. 一系列檔名(file-name),省略任何目錄分隔符。
  4. 如果路徑中最後一個檔名(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" │