6

Is it possible to create a Mixed Array in both C++ and C#

I mean an array that contains both chars and ints?

ex:

Array [][] = {{'a',1},{'b',2},{'c',3}};
BoltClock
  • 700,868
  • 160
  • 1,392
  • 1,356
sikas
  • 5,435
  • 28
  • 75
  • 120

4 Answers4

12

Neither C# nor C++ support creating this kind of data structure using native arrays, however you could create a List<Tuple<char,int>> in C# or a std::vector<std::pair<char,int>> in C++.

You could also consider using the Dictionary<> or std::map<> collections if one of the elements can be considered a unique key, and the order of the elements is unimportant but only their association.

For the case of lists (rather than dictionaries), in C# you would write:

List<Tuple<char,int>> items = new List<Tuple<char,int>>();

items.Add( new Tuple<char,int>('a', 1) );
items.Add( new Tuple<char,int>('b', 2) );
items.Add( new Tuple<char,int>('c', 3) );

and in C++ you would write:

std::vector<std::pair<char,int>> items;  // you could typedef std::pair<char,int>
items.push_back( std::pair<char,int>( 'a', 1 ) );
items.push_back( std::pair<char,int>( 'b', 2 ) );
items.push_back( std::pair<char,int>( 'c', 3 ) );
Andrew Cooper
  • 32,176
  • 5
  • 81
  • 116
LBushkin
  • 129,300
  • 32
  • 216
  • 265
  • instead of std::pair( 'a', 1 ) you could just use std::make_pair('a', 1), which the stdlib provides for your convenience :) – etarion Dec 08 '10 at 23:19
  • Why couldn't he make an array of pairs/tuples? Is there some reason I'm not seeing why his only option is to switch to List/vector? – jalf Dec 08 '10 at 23:39
  • @jalf STL-driven containers are likely to be less error-prone that generic arrays. – Yippie-Ki-Yay Dec 09 '10 at 22:14
  • True. I'm just pointing out that the "minimal" solution to the OP's problem would be to use tuple/pair. Changing the container type is irrelevant to the question, whether or not it is safer. I just think that if an answer changes the premise, it should have a good reason to do so, and explain it clearly. – jalf Dec 10 '10 at 03:40
5

In C++ you would have to use something like std::vector<boost::tuple< , , > or std::vector<std::pair> if you only have two elements in each tuple.

Example for the C++ case:

typedef std::pair<int, char> Pair;

std::vector<Pair> pairs;

pairs.push_back(Pair(0, 'c'));
pairs.push_back(Pair(1, 'a'));
pairs.push_back(Pair(42, 'b'));

Extended example for the C++ case (using boost::assign).

using boost::assign;

std::vector<Pair> pairs;

pairs += Pair(0, 'c'), Pair(1, 'a'), Pair(42, 'b');

For C# you may want to see this.

Community
  • 1
  • 1
Yippie-Ki-Yay
  • 22,026
  • 26
  • 90
  • 148
0

In C# and C++ it's not possible to create an array of mixed types. You should use other classes like std::vector in C++ or Dictionary <char, int> in C#.

Dalmas
  • 26,409
  • 9
  • 67
  • 80
0

A classic array (the one with the brackets) can only have one type, which is part of its declaration (like int[] nums). There is no Array[].

VVS
  • 19,405
  • 5
  • 46
  • 65