traBolics
traBolics

Reputation: 89

Creating multidimensional Tuple array in C++/Cli

I use this line for creating a Tuple array:

Tuple<String^, double>^ t = gcnew Tuple<String^, double>("a", 2.6);

But how can I create a multidimensional array?

E.g.:

t[][] = {
        {"a", 2.6},
        {"b", 7.1},
        {"c", 2.4},
        {"d", 2.7}
};

Even if we do that, how can I get elements inside of array, e.g.:

for (int i = 0; i < 4; i++){
        textBox1->Text += t[i][0];
}

Upvotes: 1

Views: 1700

Answers (1)

yuyoyuppe
yuyoyuppe

Reputation: 1602

Use MSDN.

using namespace System;

int main()
{
  const unsigned rank = 2;
  const unsigned dim1 = 3;
  const unsigned dim2 = 4;

  auto arr = gcnew array<Tuple<String ^, double> ^, rank>(dim1, dim2);

  for(int i = 0; i < dim1; i++)
    for(int j = 0; j < dim2; j++)
      arr[i, j] = gcnew Tuple<String ^, double>("@_@", i * j);

  return 0;
}

rank is a dimensionality of array, so in this case it's a 2d array.

UPD

using aggregate initialization:

using elemT = Tuple<String ^, double>; // somewhere at top-level

  //...
  auto arr =
    gcnew array<elemT ^, rank>{{gcnew elemT{"@_@", 1.}, gcnew elemT{"^_^", 2.}},
                               {gcnew elemT{"~_~", 3.}, gcnew elemT{"+_+", 4.}}};

UPD2

As the mystery unveiled, OP didn't need multidimensional arrays, just an array of tuples:

#include <iostream>

using namespace System;
using subarrT = array<double>;
using elemT   = Tuple<String ^, String ^, subarrT ^>;
int main()
{
  auto arr = gcnew array<elemT ^>{
    gcnew elemT{"Name1", "Surname1", gcnew subarrT{29., 123., 10., 1230.}},
    gcnew elemT{"Name2", "Surname2", gcnew subarrT{49., 32., 8., 256.}},
  };
  std::cout << static_cast<double>(arr[0]->Item3[1]) << std::endl; // 123.
  return 0;
}

Upvotes: 2

Related Questions