Initialize static Dictionary while creating in C++/CLI

老子叫甜甜 提交于 2019-12-04 02:40:10
KeithS

C# 3.0 and later allows users to define an "initializer"; for collections, that's a series of elements, which for Dictionaries is streamlined to keys and values. C++.NET to my knowledge does not have this language feature. See this question: it's very similar: Array initialization in Managed C++. Array initializers are the ONLY such initializer in C++; other collections do not offer them in C++.

Basically, your main option is to declare a static constructor and initialize your dictionary in there.

This type of Dictionary<T> initialization is a feature not of the class itself, but of the C# compiler. It translates it into separate statements for creation of the Dictionary<T> object, and the creation and addition of each key/value pair. I don't believe the C++ compiler offers the same capabilities.

My approach is (.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;
}

It is possible! :-)
Not straight-forward, but with a tiny helper function you can create and init a dictionary in one line of code:

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

Tested, working.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!