C++ 字典可以在c++中使用吗

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16417463/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 20:19:46  来源:igfitidea点击:

Can dictionaries be used in c++

c++dictionary

提问by Elliott

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. 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.

我一直在 C# 中查找字典,它们似乎非常有用,并且想知道是否可以在 C++ 中使用它们,因为我试图在 C++ 中搜索字典但似乎没有我可以找到。是否有某种库可以下载并包含到项目中,或者是否有一个函数可以使用不同的名称执行相同的操作。

回答by Anders Abel

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. std::mapis internally based on a red-black tree datastructure, while Dictionaryuses a hash table internally.

它具有与 .NET Dictionary 相同的基本功能,但实现却大不相同。std::map内部基于红黑树数据结构,Dictionary内部使用哈希表。

If you're just looking for something with the same behaviour, std::mapwill do, but if you have large amounts of data you have to be aware of the different performance characteristics.

如果您只是在寻找具有相同行为的东西,std::map可以这样做,但是如果您有大量数据,则必须了解不同的性能特征。

回答by Angew is no longer proud of SO

There's std::mapfor logarithmic access time (usually based on a tree implementation) and std::unordered_map(since C++11) for expected constant, worst-case linear access time (usually based on a hashing implementation).

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

回答by ForEveR

std::mapis like a Dictionary.

std::map就像一个Dictionary.

回答by Dharmendra Mishra

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"}}