Is it possible to create a static dictionary in a C ++ / CLI environment?

I used a static array of a C ++ / CLI project, for example:

static array < array< String^>^>^ myarray=
{
  {"StringA"},
  {"StringB"}
};

Is it possible to create a dictionary in the same way? I could not create and initialize it.

static Dictionary< String^, String^>^ myDic=
{
  {"StringA", "1"},
  {"StringB", "2"}
};
+3
source share
6 answers

An example of your dictionary and others are called Collection Initializerin C #.

You cannot do this in C ++ / CLI.

// C# 3.0
class Program
{
    static Dictionary<int, string> dict = new Dictionary<int, string>
    {
        {1, "hello"},
        {2, "goodbye"}
    };

    static void Main(string[] args)
    {
    }
}
+2
source

You cannot do this directly in the declaration, but you can use the static constructor to initialize once when your static constructor calls the method Add().

+1
source

CLI, , ?

#include <iostream>

int main() {
  static int a[][3] = { {1, 2, 3}, {4, 5, 6}};
  std::cout << a[0][0] << " " << a[1][0];
  //..

}
0

++ std::map , :

template <class InputIterator>
  map ( InputIterator first, InputIterator last,
        const Compare& comp = Compare(), const Allocator& = Allocator() );

- , binary_search. , .

0

(.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;
}
0

Also see. Fooobar.com/questions/656047 / ... . In C ++ / CLI, it is not possible to use compiler functions such as C # as written by other users.
So I created a little helper function (see linked post), similar to @MrHIDEn's approach here, but when solving it, it seems to use fixed values.

0
source

Source: https://habr.com/ru/post/1735082/


All Articles