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}};
1
  • 1
    A char is an integer type in C++ Dec 8, 2010 at 23:32

4 Answers 4

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 ) );
4
  • instead of std::pair<char,int>( 'a', 1 ) you could just use std::make_pair('a', 1), which the stdlib provides for your convenience :)
    – etarion
    Dec 8, 2010 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 8, 2010 at 23:39
  • @jalf STL-driven containers are likely to be less error-prone that generic arrays. Dec 9, 2010 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, 2010 at 3: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.

2
  • can you give an example on how to achieve this?
    – sikas
    Dec 8, 2010 at 22:34
  • std::vector<std::pair> preferred
    – Svisstack
    Dec 8, 2010 at 22:34
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#.

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[].

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.