通过参数作为值返回还是作为引用返回?

问题描述

std::vector<std::string> get() {
    std::vector<string> result {};
    
    for (int i = 0; i < 10000) {
        result.emplace_back("test" + to_string(i));
    }

    return result;
}

//or
void get(std::vector<std::string>& result) {
    
    for (int i = 0; i < 10000) {
        result.emplace_back("test" + to_string(i));
    }
}

//遍历引用比返回引用效率更高?还是复制省略,在效率上是一样的吗?

int main()
{
    auto arr = get();
    std::vector<std::string> arrPassingByRef{};
    get(arrPassingByRef);
}

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)