在C ++和C#混合阵列 [英] Mixed Arrays in C++ and C#

查看:418
本文介绍了在C ++和C#混合阵列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有可能在C ++和创建一个混合阵列C#

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

例如:

 数组[] [] = {{'A',1},{'B',2},{'C',3}};


解决方案

无论是C#,也没有C ++支持使用本机阵列,但您可以创建创建这种数据结构的列表与LT元组LT; CHAR,INT和GT;> 在C#或的std ::矢量<的std ::对< CHAR,INT和GT;> 在C ++中。

您也可以考虑使用词典<> 的std ::地图<>如果一个集合的元素可以被认为是一个唯一的密钥,和所述元素的顺序是不重要的,但只有它们的关联。

有关名单(而非字典),在C#中,你会写的情况:

 列表与LT元组LT; CHAR,INT和GT;>项目=新的List&LT元组LT; CHAR,INT和GT;>();items.Add(新行<焦炭,INT>('A',1));
items.Add(新行<焦炭,INT>('B',2));
items.Add(新行<焦炭,INT>('C',3));

和C ++中你可以这样写:

 的std ::矢量<的std ::对< CHAR,INT和GT;>项目; //你可以的typedef的std ::对<焦炭,INT>
items.push_back(性病::对<焦炭,INT>('A',1));
items.push_back(性病::对<焦炭,INT>('B',2));
items.push_back(性病::对<焦炭,INT>('C',3));

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

解决方案

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

这篇关于在C ++和C#混合阵列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆