C ++和C#中的混合数组

时间:2022-09-01 19:20:40

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

是否可以在C ++和C#中创建混合数组

I mean an array that contains both chars and ints?

我的意思是一个包含字符和整数的数组?

ex:

Array [][] = {{'a',1},{'b',2},{'c',3}};

4 个解决方案

#1


9  

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++.

C#和C ++都不支持使用本机数组创建这种数据结构,但是你可以在C#中创建一个List >或者在C ++中创建一个std :: vector > 。

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.

如果其中一个元素可以被视为唯一键,您也可以考虑使用Dictionary <>或std :: map <>集合,并且元素的顺序不重要但只是它们的关联。

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

对于列表(而不是字典)的情况,在C#中你会写:

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:

在C ++中你会写:

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

#2


3  

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.

在C ++中,如果每个元组中只有两个元素,则必须使用类似std :: vector 或std :: vector 的东西。

Example for the C++ case:

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

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

C ++案例的扩展示例(使用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.

对于C#,你可能想看到这个。

#3


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#.

在C#和C ++中,不可能创建混合类型的数组。您应该使用其他类,如C ++中的std :: vector或C#中的Dictionary ,int>

#4


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

经典数组(带括号的数组)只能有一种类型,这是其声明的一部分(如int [] nums)。没有Array []。

#1


9  

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++.

C#和C ++都不支持使用本机数组创建这种数据结构,但是你可以在C#中创建一个List >或者在C ++中创建一个std :: vector > 。

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.

如果其中一个元素可以被视为唯一键,您也可以考虑使用Dictionary <>或std :: map <>集合,并且元素的顺序不重要但只是它们的关联。

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

对于列表(而不是字典)的情况,在C#中你会写:

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:

在C ++中你会写:

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

#2


3  

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.

在C ++中,如果每个元组中只有两个元素,则必须使用类似std :: vector 或std :: vector 的东西。

Example for the C++ case:

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

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

C ++案例的扩展示例(使用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.

对于C#,你可能想看到这个。

#3


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#.

在C#和C ++中,不可能创建混合类型的数组。您应该使用其他类,如C ++中的std :: vector或C#中的Dictionary ,int>

#4


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

经典数组(带括号的数组)只能有一种类型,这是其声明的一部分(如int [] nums)。没有Array []。