`std::filesystem::path` 没有反向迭代器?
No reverse iterators for `std::filesystem::path`?
std::filesystem::path
不提供反向迭代器(即 rbegin
和 rend
)是否有技术原因?
如果我有 std::filesystem::path
对应 /a/b/c/b/d/b/e
并且我想找到匹配 b
的第一个组件,我可以使用 std::find(p.begin(), p.end(), fs::path("b"))
.
但是如果我想找到匹配 b
的 last 组件,我不能只切换到反向迭代器。我可以编写自己的循环,但这似乎是一个“几乎可以免费”实现的常见操作。
接口的设计是否有什么东西会导致难以提供反向迭代器?
根据cppreference.com的这一页:
"std::reverse_iterator does not work with iterators whose dereference returns a reference to a member of *this (so-called "stashing iterators"). An example of a stashing iterator is std::filesystem::path::iterator."
同样来自 boost.org 的页面,上面写着:
Path iterators store their value objects internally and when dereferenced return references to those internal objects. They cannot be used with iterator adaptors such as std::reverse_iterator that assume references obtained by dereferencing an iterator point to objects that out-live the iterator itself.
要查找有关隐藏迭代器的更详细说明,请访问 。
std::filesystem::path
不提供反向迭代器(即 rbegin
和 rend
)是否有技术原因?
如果我有 std::filesystem::path
对应 /a/b/c/b/d/b/e
并且我想找到匹配 b
的第一个组件,我可以使用 std::find(p.begin(), p.end(), fs::path("b"))
.
但是如果我想找到匹配 b
的 last 组件,我不能只切换到反向迭代器。我可以编写自己的循环,但这似乎是一个“几乎可以免费”实现的常见操作。
接口的设计是否有什么东西会导致难以提供反向迭代器?
根据cppreference.com的这一页:
"std::reverse_iterator does not work with iterators whose dereference returns a reference to a member of *this (so-called "stashing iterators"). An example of a stashing iterator is std::filesystem::path::iterator."
同样来自 boost.org 的页面,上面写着:
Path iterators store their value objects internally and when dereferenced return references to those internal objects. They cannot be used with iterator adaptors such as std::reverse_iterator that assume references obtained by dereferencing an iterator point to objects that out-live the iterator itself.
要查找有关隐藏迭代器的更详细说明,请访问