1

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];
}

1 Answer 1

2

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;
}
Sign up to request clarification or add additional context in comments.

9 Comments

Hi, can we initialize the arr values without using for loop?
Sorry about the bothering again. I connot managed to do this e.g: arr= { {"a", 2.6}, {"b", 7.1}, {"c", 2.4}, {"d", 2.7} };
Your last update is arr={ { {"a",1},{"b",2} },{ {"c",1},{"d",2} } }
First dimension store 4 secondary arrays and secondary dimesion store the string and double values. That is I'm trying to do
auto Tablo = gcnew array<elemT ^, 2> { { gcnew elemT{ "Kerem Kaçkar", "Antibiyotik", 29, 123, 10, 0 }, gcnew elemT{ "Nergis Kara", "Serum", 49, 32, 8, 0 }, gcnew elemT{ "Füsun Tuzcu", "Medikal", 23, 7, 11, 0 } } };
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.