是否有与此 python 语句等效的 C++11:
x, y, z = three_value_array
在 C++ 中,你可以这样做:
double x, y, z;
std::array<double, 3> three_value_array;
// assign values to three_value_array
x = three_value_array[0];
y = three_value_array[1];
z = three_value_array[2];
在 C++11 中是否有更紧凑的方法来实现此目的?
您可以使用std::tuple
and std::tie
以此目的:
#include <iostream>
#include <tuple>
int main()
{
/* This is the three-value-array: */
std::tuple<int,double,int> triple { 4, 2.3, 8 };
int i1,i2;
double d;
/* This is what corresponds to x,y,z = three_value_array: */
std::tie(i1,d,i2) = triple;
/* Confirm that it worked: */
std::cout << i1 << ", " << d << ", " << i2 << std::endl;
return 0;
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)