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.
问题:
回答1:
There is a corresponding type in STL, that's called std::map
.
It has the same basic functionality as a .NET Dictionary, but the implementation is quite different. std::map
is internally based on a red-black tree datastructure, while Dictionary
uses a hash table internally.
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.
回答2:
There's std::map
for 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).
回答3:
std::map is like a Dictionary
.
回答4:
We can use map in C++ The basic format of using a map is -
std::map<Key_type, Value_Type> ;
Example -
std::map<std::string,std::string> x = {{"A","ABC"},{"B","DEF"}}