小编Har*_*ake的帖子

编译器错误?在 std::tuple 中返回 std::vector 和 std::string。但我得到了奇怪的价值观

我想返回多个值并使用 声明该函数auto

但这效果并不好。无法正确返回值。它被覆盖了。

我尝试执行以下功能f1f3。这些函数应该返回元组中的向量和字符串。但只是f3效果很好。

#include <iostream>
#include <vector>
#include <string>
#include <tuple>

auto f1(){
    std::vector<double> v(10, 0);
    std::string s = "hello";
    return std::forward_as_tuple(v, s);
}

auto f2(){
    std::vector<double> v(10, 0);
    return std::forward_as_tuple(v, "hello");
}

std::tuple<std::vector<double>, std::string> f3(){
    std::vector<double> v(10, 0);
    std::string s = "hello";
    return std::forward_as_tuple(v, s);   
}
int main(void){
    //change the function
    //auto [vec, str] = f1();
    //auto [vec, str] = f2();
    auto [vec, str] = f2();

    for (auto e : …
Run Code Online (Sandbox Code Playgroud)

c++ stdvector auto stdtuple c++17

-1
推荐指数
1
解决办法
206
查看次数

标签 统计

auto ×1

c++ ×1

c++17 ×1

stdtuple ×1

stdvector ×1