C++/CLI 词典词典
C++/CLI Dictionary of Dictionaries
有没有办法在 C++ CLI 中创建字典的字典?我以前使用的是字符串字典、ArrayList 对,发现将我的 ArrayList 转换为字符串字典、CustomValueStruct 对而不是使用键匹配很有用。
我的字符串字典、数组列表对的代码:
Dictionary<String^, ArrayList^> myDictionary = gcnew Dictionary<String^, ArrayList^>();
如何将其转换为使用字典词典?
我尝试使用但无法编译的代码是:
Dictionary<String^, Dictionary<String^, CustomValueStruct>> myDictionary = gcnew Dictionary<String^, Dictionary<String^, CustomValueStruct>>();
其中 CustomValueStruct
只是我代码中的一个自定义结构,它以前存在于 ArrayList 中。
尝试行的结果是:
error C3225: generic type argument for 'TValue' cannot be 'System::Collections::Generic::Dictionary<TKey,TValue>', it must be a value type or a handle to a reference type
正如编译器所说,你忘记了一个 ^
:
auto myDictionary = gcnew Dictionary<String^, Dictionary<String^, CustomValueStruct>^>();
请注意,将项目添加到 myDictionary
时需要创建一个 Dictionary<String^, CustomValueStruct>
:
myDictionary.Add("Key", gcnew Dictionary<String^, CustomValueStruct>());
最后,确定你真的希望 CustomValueStruct
是一个值类型;你通常最好写
ref class CustomRefClass { /* ... */ };
这又需要 ^
用于嵌套 Dictionary
:
auto myDictionary = gcnew Dictionary<String^, Dictionary<String^, CustomRefClass^>^>();
有没有办法在 C++ CLI 中创建字典的字典?我以前使用的是字符串字典、ArrayList 对,发现将我的 ArrayList 转换为字符串字典、CustomValueStruct 对而不是使用键匹配很有用。
我的字符串字典、数组列表对的代码:
Dictionary<String^, ArrayList^> myDictionary = gcnew Dictionary<String^, ArrayList^>();
如何将其转换为使用字典词典?
我尝试使用但无法编译的代码是:
Dictionary<String^, Dictionary<String^, CustomValueStruct>> myDictionary = gcnew Dictionary<String^, Dictionary<String^, CustomValueStruct>>();
其中 CustomValueStruct
只是我代码中的一个自定义结构,它以前存在于 ArrayList 中。
尝试行的结果是:
error C3225: generic type argument for 'TValue' cannot be 'System::Collections::Generic::Dictionary<TKey,TValue>', it must be a value type or a handle to a reference type
正如编译器所说,你忘记了一个 ^
:
auto myDictionary = gcnew Dictionary<String^, Dictionary<String^, CustomValueStruct>^>();
请注意,将项目添加到 myDictionary
时需要创建一个 Dictionary<String^, CustomValueStruct>
:
myDictionary.Add("Key", gcnew Dictionary<String^, CustomValueStruct>());
最后,确定你真的希望 CustomValueStruct
是一个值类型;你通常最好写
ref class CustomRefClass { /* ... */ };
这又需要 ^
用于嵌套 Dictionary
:
auto myDictionary = gcnew Dictionary<String^, Dictionary<String^, CustomRefClass^>^>();