Mixed Arrays in C++ and C#

前端 未结 4 405
心在旅途
心在旅途 2021-01-03 03:21

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         


        
4条回答
  •  北荒
    北荒 (楼主)
    2021-01-03 03:53

    Neither C# nor C++ support creating this kind of data structure using native arrays, however you could create a List> in C# or a std::vector> 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> items = new List>();
    
    items.Add( new Tuple('a', 1) );
    items.Add( new Tuple('b', 2) );
    items.Add( new Tuple('c', 3) );
    

    and in C++ you would write:

    std::vector> items;  // you could typedef std::pair
    items.push_back( std::pair( 'a', 1 ) );
    items.push_back( std::pair( 'b', 2 ) );
    items.push_back( std::pair( 'c', 3 ) );
    

提交回复
热议问题