Mixed arrays in C ++ and C #

Is it possible to create a mixed array in both C ++ and C #

I mean an array containing both characters and ints?

Example:

Array [][] = {{'a',1},{'b',2},{'c',3}};
+5
source share
4 answers

Neither C # nor C ++ supports the creation of such a data structure using inline arrays , however you can create List<Tuple<char,int>>in C # or std::vector<std::pair<char,int>>C ++.

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

In the case of lists (not dictionaries) in C #, you should 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 should 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 ) );
+9

C++ - std::vector<boost::tuple< , , > std::vector<std::pair>, .

C++:

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'));

C++ ( boost::assign).

using boost::assign;

std::vector<Pair> pairs;

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

C# .

+3

# ++ . , std::vector ++ Dictionary < char, int > #.

0

(, ) , (, int [] nums). [].

0