【问题标题】:Is it possible to create a static dictionary in a C++/CLI environment?是否可以在 C++/CLI 环境中创建静态字典?
【发布时间】:2010-03-02 16:41:22
【问题描述】:

我在我的 C++/CLI 项目中使用的静态数组是这样的:

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

是否可以以同样的方式创建字典? 我无法创建和初始化一个。

static Dictionary< String^, String^>^ myDic=
{
  {"StringA", "1"},
  {"StringB", "2"}
};

【问题讨论】:

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


    【解决方案1】:

    您的字典示例和其他示例在 C# 中称为 Collection Initializer

    你不能在 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】:

      您不能直接在声明时执行此操作,但您可以使用静态构造函数通过让静态构造函数调用Add() 方法来进行一次初始化。

      【讨论】:

        【解决方案3】:

        我不了解 CLI,但二维数组不应该非常接近您想要的吗?

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

        【讨论】:

          【解决方案4】:

          虽然在 C++ 中您无法创建 std::map 并将其初始化为数组,但您可以使用此构造函数在构造时加载地图:

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

          另一种方法是使用数组和搜索方法,例如binary_search。如果数据没有变化,这可能很有用。

          【讨论】:

            【解决方案5】:

            我的方法是 (.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;
            }
            

            【讨论】:

              【解决方案6】:

              另见this Stackoverflow post。在 C++/CLI 中,无法像其他人所写的那样使用 C# 中的编译器功能。
              因此,我创建了一个小辅助函数(参见链接的帖子),类似于此处@MrHIDEn 的方法,但在他的解决方案中,他似乎使用了固定值。

              【讨论】:

                【解决方案7】:

                在 C++ 中:

                #include <map>
                
                std::map< int, std::string > status_to_str {
                  {0, "failure"},
                  {1, "none"},
                  {2, "success"},
                };
                
                void print_status(int status) {
                  std::cout<< status_to_str[status] << std::endl;
                }
                

                【讨论】:

                  猜你喜欢
                  • 2011-08-26
                  • 2011-11-09
                  • 1970-01-01
                  • 2010-10-02
                  • 1970-01-01
                  • 2020-06-20
                  • 1970-01-01
                  • 2012-01-20
                  • 2012-04-26
                  相关资源
                  最近更新 更多