C++:将字符串作为常量引用是否经常被认为是一种好习惯?

这是我的 C++ 代码中的一个片段:

std::queue<std::string> get_file_names(const std::string &indir)
{
    std::queue<std::string> file_names;

    fs::recursive_directory_iterator end;
    for (fs::recursive_directory_iterator it(indir); it != end; it++) {
        const std::string &extn = it->path().extension().string();
        if (extn == ".zip") {
            const std::string &file_name = it->path().string();
            file_names.push(file_name);
        }
    }

    return file_names;
}

使每个字符串都不会修改 const 引用是一个好习惯吗?我无法理解在这种情况下如何存在这样的参考。就像it->path().string()上面的返回值一样。当推回向量时,如何将其分配给以后可以在函数范围之外使用的引用?

我觉得它必须与std::move.

以上是C++:将字符串作为常量引用是否经常被认为是一种好习惯?的全部内容。
THE END
分享
二维码
< <上一篇
下一篇>>