C ++ CLI集合初始化语法 [英] C++ CLI Collection initializer syntax

查看:218
本文介绍了C ++ CLI集合初始化语法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是C ++ CLI支持的吗?我想在C ++ CLI中做类似下面的C#示例

Is this supported in C++ CLI? I want to do something like the following C# example in C++ CLI

var dictionary = new Dictionary<string, string> { { "foo", "bar" } };

感谢

推荐答案

我想出的最好的是创建一个数组初始化的内联,然后初始化字典与数组的内容在一个静态构造函数。类似

The best I came up with was creating an array initialized inline, then initializing the dictionary with the contents of the array in a static constructor. Something like

static initonly System::Collections::Generic::Dictionary<System::String^, System::String^>^ dictionary;
static initonly array<System::String^> arrayToPopulateDictionary = gcnew array<System::String^> { "foo", "bar" };

static Foo()
{
    for (int i  = 0; i < arrayToPopulateDictionary->Length; i += 2)
		listMappings->Add(arrayToPopulateDictionary[i], arrayToPopulateDictionary[i + 1]));
}

这篇关于C ++ CLI集合初始化语法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆