简体   繁体   English

可以在c ++中使用字典

[英]Can dictionaries be used in c++

I have been looking up dictionaries in C# and they seem to be highly useful, and was wondering if it is possible to use them in C++ as I have tried to search for dictionaries in C++ but there doesn't seem to be an equivalent that I can find. 我一直在查找C#中的字典,它们似乎非常有用,并且想知道是否可以在C ++中使用它们,因为我试图在C ++中搜索字典但是似乎没有相应的我可以找到。 Is there some sort of library that I could download and include to the project or is there a function which does the same thing just with a different name. 是否有某种类型的库我可以下载并包含在项目中,或者是否有一个函数只使用不同的名称来执行相同的操作。

There is a corresponding type in STL, that's called std::map . STL中有一个相应的类型,称为std::map

It has the same basic functionality as a .NET Dictionary, but the implementation is quite different. 它具有与.NET Dictionary相同的基本功能,但实现方式完全不同。 std::map is internally based on a red-black tree datastructure, while Dictionary uses a hash table internally. std::map在内部基于红黑树数据结构,而Dictionary在内部使用哈希表。

If you're just looking for something with the same behaviour, std::map will do, but if you have large amounts of data you have to be aware of the different performance characteristics. 如果你只是在寻找具有相同行为的东西, std::map就可以了,但是如果你有大量的数据,你必须要注意不同的性能特征。

对于对数访问时间(通常基于树实现)和std::unordered_map (自C ++ 11以来)的std::map用于预期的常量,最坏情况线性访问时间(通常基于散列实现)。

std :: map就像一个Dictionary

We can use map in C++ The basic format of using a map is - 我们可以在C ++中使用map使用map的基本格式是 -

    std::map<Key_type, Value_Type> ;

Example - 示例 -

    std::map<std::string,std::string> x = {{"A","ABC"},{"B","DEF"}}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM