gpt4 book ai didi

iterator - boost::filesystem::directory_iterator 是否因删除而失效?

转载 作者:行者123 更新时间:2023-12-02 21:35:13 26 4
gpt4 key购买 nike

我正在遍历一个目录,当某个项目符合某些条件时,我将其删除。我可以在循环内安全地执行此操作,还是必须将路径保存在数组中并稍后删除?

我在boost::filesystem docs中没有找到相关信息.

最佳答案

引用 docs of boost::filesystem::directory_iterator 所附注释的第一部分(重点是我自己的):

Programs performing directory iteration may wish to test if the path obtained by dereferencing a directory iterator actually exists. It could be a symbolic link to a non-existent file. Programs recursively walking directory trees for purposes of removing and renaming entries may wish to avoid following symbolic links.

我清楚地意识到,为了删除文件而迭代目录是官方支持的用例,因此不会使迭代器无效。另外,引用该注释的第二部分:

If a file is removed from or added to a directory after the construction of a directory_iterator for the directory, it is unspecified whether or not subsequent incrementing of the iterator will ever result in an iterator whose value is the removed or added directory entry. See ISO/IEC 9945 readdir_r().

这是一个非常具体的声明,关于在迭代目录时是否会出现已删除的文件。再说一遍,我明白迭代过程本身在任何情况下仍然有效。

请注意ISO/IEC 9945有类似的措辞。

关于iterator - boost::filesystem::directory_iterator 是否因删除而失效?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13513369/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com