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.
You can create a dictionary in C, but there is no dictionary built in to the standard C library. A quick search on Google code shows that there are open-source (and generously licensed) C dictionary implementations here and here. Save this answer. Show activity on this post.
A dictionary is defined as a general-purpose data structure for storing a group of objects. A dictionary is associated with a set of keys and each key has a single associated value. When presented with a key, the dictionary will simply return the associated value.
A fast hash map/hash table (whatever you want to call it) for the C programming language. It can associate a key with a pointer or integer value in O(1) time.
Let's start with the map function which we quite often use in different embedded systems applications. The map function is commonly used as a built-in Arduino C function and it's very handy in a wide range of applications.
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.
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).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With