.net 在C+中创建时初始化静态字典+/CLI

.net 在C+中创建时初始化静态字典+/CLI,.net,dictionary,static,c++-cli,initialization,.net,Dictionary,Static,C++ Cli,Initialization,今天我看到了创建静态字典并对其进行初始化的C代码: public static readonly Dictionary<string, string> dict = new Dictionary<string, string>() { {"br","value1"}, {"cn","value2"}, {"de","value3"}, }; 公共静态只读字典dict=

今天我看到了创建静态字典并对其进行初始化的C代码:

public static readonly Dictionary<string, string> dict = new Dictionary<string, string>()
        {
            {"br","value1"},
            {"cn","value2"},
            {"de","value3"},
        };
公共静态只读字典dict=newdictionary() { {“br”,“value1”}, {“cn”,“value2”}, {“de”,“value3”}, }; 但当我决定为C++/CLI编写相同的代码时,出现了一个错误。以下是我的尝试:

static System::Collections::Generic::Dictionary<System::String^, System::String^>^ dict = gcnew System::Collections::Generic::Dictionary<System::String^, System::String^>( )
    {
        {"br","value1"},
        {"cn","value2"},
        {"de","value3"},
    };
static System::Collections::Generic::Dictionary^dict=gcnew System::Collections::Generic::Dictionary()
{
{“br”,“value1”},
{“cn”,“value2”},
{“de”,“value3”},
};

我可以这样做吗?如果可以,怎么做?

这种类型的
字典
初始化不是类本身的功能,而是C#编译器的功能。它将其转换为单独的语句,用于创建
字典
对象,以及创建和添加每个键/值对。我不相信C++编译器提供了相同的功能。

< P> C 3和以后允许用户定义一个“初始化器”;对于集合,这是一系列元素,对于字典,这些元素被简化为键和值。C++对我的知识没有这种语言特征。看这个问题:非常相似:。数组初始化器是C++中唯一的初始化器;其他集合不提供它们在C++中。

基本上,您的主要选择是声明一个静态构造函数并在其中初始化字典。

我的方法是(.NET 4.5):

//file.h
使用名称空间系统;
使用命名空间System::Collections::Generic;
//某类
public://or 私人:
静态字典^dict=dictionalizer();
私人:
静态字典^Dictionary Initializer();
//file.cpp
#包括“file.h”
字典^SomeClass::字典初始化器(){
字典^dict=gc新字典;
dict->Add(“br”,“value1”);
记录->添加(“cn”、“价值2”);
dict->Add(“de”,“value3”);
返回命令;
}
这是可能的!:-)
不是直截了当的,但是通过一个很小的助手函数,您可以在一行代码中创建和初始化字典:

// helper class
generic <class TKey, class TValue>
ref class CDict
{
public:
  static Dictionary<TKey, TValue>^ CreateDictionary (...array<KeyValuePair<TKey, TValue>>^ i_aValues)
  {
    Dictionary<TKey, TValue>^ dict = gcnew Dictionary<TKey, TValue>;
    for (int ixCnt = 0; ixCnt < (i_aValues ? i_aValues->Length : 0); ixCnt++)
      dict->Add (i_aValues[ixCnt].Key, i_aValues[ixCnt].Value);
    return dict;
  }
};

// Test
ref class CTest
{
public:
  static Dictionary<int, String^>^ ms_dict = CDict<int, String^>::CreateDictionary (gcnew array<KeyValuePair<int, String^>>
  {
    KeyValuePair<int, String^>(1, "A"),
    KeyValuePair<int, String^>(2, "B")
  });
};

int main()
{
  for each (KeyValuePair<int, String^> kvp in CTest::ms_dict)
    Console::WriteLine (kvp.Key.ToString() + " " + kvp.Value);
}
//助手类
通用的
参考等级CDict
{
公众:
静态字典^CreateDictionary(…数组^i\u aValues)
{
字典^dict=gc新字典;
对于(int-ixCnt=0;ixCnt<(i_-aValues?i_-aValues->Length:0);ixCnt++)
dict->Add(i_aValues[ixCnt].Key,i_aValues[ixCnt].Value);
返回命令;
}
};
//试验
参考等级CTest
{
公众:
静态字典^ms_dict=CDict::CreateDictionary(新数组
{
KeyValuePair(1,“A”),
KeyValuePair(2,“B”)
});
};
int main()
{
对于每个(CTest::ms_dict中的KeyValuePair kvp)
控制台::WriteLine(kvp.Key.ToString()+“”+kvp.Value);
}

经过测试,正常工作。

可能的重复意味着我不能像在C#中那样初始化它?基本上,是的。这是C语言的一个语言特性,在托管C++中是不可用的。“对,它需要做得不同”,但是有没有办法?看看基思的答案,寻找数组类似的链接。对于字典,我认为你必须逐行创建和添加。
// helper class
generic <class TKey, class TValue>
ref class CDict
{
public:
  static Dictionary<TKey, TValue>^ CreateDictionary (...array<KeyValuePair<TKey, TValue>>^ i_aValues)
  {
    Dictionary<TKey, TValue>^ dict = gcnew Dictionary<TKey, TValue>;
    for (int ixCnt = 0; ixCnt < (i_aValues ? i_aValues->Length : 0); ixCnt++)
      dict->Add (i_aValues[ixCnt].Key, i_aValues[ixCnt].Value);
    return dict;
  }
};

// Test
ref class CTest
{
public:
  static Dictionary<int, String^>^ ms_dict = CDict<int, String^>::CreateDictionary (gcnew array<KeyValuePair<int, String^>>
  {
    KeyValuePair<int, String^>(1, "A"),
    KeyValuePair<int, String^>(2, "B")
  });
};

int main()
{
  for each (KeyValuePair<int, String^> kvp in CTest::ms_dict)
    Console::WriteLine (kvp.Key.ToString() + " " + kvp.Value);
}