结构化绑定提供了多返回值的功能,这是C++17提供的新语法糖。
#include <iostream>
#include <tuple>
std::tuple<int, double, std::string> f() {
return make_tuple(1, 1.0, "test");
}
int main() {
auto [x, y, z] = f();
std::cout << x << ", " << y << "," << z;
return 0;
}
1, 1,test
#include <tuple>
#include <iostream>
int main() {
std::tuple<int, float, std::string> myTuple(42, 3.14f, "Hello");
int a;
float b;
std::string c;
std::tie(a, b, c) = myTuple;
std::cout << "a: " << a << std::endl;
std::cout << "b: " << b << std::endl;
std::cout << "c: " << c << std::endl;
return 0;
}
No comments yet.