10

この 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 でこれを達成するためのよりコンパクトな方法はありますか?

4

1 に答える 1

11

この目的std::tupleで とを使用できます。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;
}
于 2012-11-09T02:48:36.893 に答える