【问题标题】:Initialize static Dictionary while creating in C++/CLI在 C++/CLI 中创建时初始化静态字典
【发布时间】:2011-08-26 15:54:38
【问题描述】:

今天我看到了创建静态字典并初始化它的 C# 代码:

public static readonly Dictionary<string, string> dict = new Dictionary<string, string>()
        {
            {"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"},
    };

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

【问题讨论】:

标签: .net dictionary static c++-cli initialization


【解决方案1】:

有可能! :-)
不是直截了当,但是通过一个很小的辅助函数,您可以在一行代码中创建和初始化字典:

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

经过测试,工作正常。

【讨论】:

    【解决方案2】:

    我的方法是(.NET 4.5):

    // file.h
    using namespace System;
    using namespace System::Collections::Generic;
    // SomeClass
    public://or private:
        static Dictionary<String^, String^>^ dict = dictInitializer();
    private:
        static Dictionary<String^, String^>^ dictInitializer();
    
    // file.cpp
    #include "file.h"
    Dictionary<String^, String^>^ SomeClass::dictInitializer(){
        Dictionary<String^, String^>^ dict = gcnew Dictionary<String^, String^>;
        dict->Add("br","value1");
        dict->Add("cn","value2");
        dict->Add("de","value3");
        return dict;
    }
    

    【讨论】:

      【解决方案3】:

      C# 3.0 及更高版本允许用户定义“初始化程序”;对于集合,这是一系列元素,对于字典,这些元素被简化为键和值。据我所知,C++.NET 没有这种语言功能。看到这个问题:非常相似:Array initialization in Managed C++。数组初始值设定项是 C++ 中唯一的此类初始值设定项;其他集合不提供它们在 C++ 中。

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

      【讨论】:

        【解决方案4】:

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

        【讨论】:

        • 意思是我不能像在C#中那样初始化它?
        • 基本上,是的。这是托管 C++ 中没有的 C# 语言特性。
        • “对,它需要以不同的方式完成”但是有没有办法?
        • 请参阅 Keith 的答案以获取指向数组类似内容的链接。对于字典,我认为您必须逐行创建和添加。
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-09-05
        • 1970-01-01
        • 1970-01-01
        • 2023-03-31
        • 1970-01-01
        相关资源
        最近更新 更多