c++17 多返回值处理

第一种方式

自定义结构体加结构化绑定

#include <iostream>
using namespace std;

auto foo() {
  struct retVals {        // Declare a local structure 
    int i1, i2;
    string str;
  };
  return retVals {10, 20, "Hi"}; // Return the local structure
}

int main() {
  auto [value1, value2, value3] = foo(); // structured binding declaration
  cout << value1 << ", " << value2 << ", " << value3 << endl;
}

第二种方式

tuple加结构化绑定

#include <iostream>
#include <tuple>

using namespace std;

tuple<int, float, string> foo()
{
  return {128, 3.142, "Hello"};
}

int main()
{
  auto [value1, value2, value3] = foo();
  cout << value1 << ", " << value2 << ", " << value3 << endl;
}
posted @ 2021-09-08 21:31  yuzuki_n  阅读(370)  评论(0)    收藏  举报