Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can dictionaries be used in c++

Tags:

c++

dictionary

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.

like image 472
Elliott Avatar asked May 07 '13 11:05

Elliott


People also ask

Do dictionaries exist in C?

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.

What are dictionaries in C?

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.

Does C have Hashmap?

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.

Can we use map in C?

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.


2 Answers

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.

like image 183
Anders Abel Avatar answered Sep 30 '22 20:09

Anders Abel


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

like image 25
Angew is no longer proud of SO Avatar answered Sep 30 '22 20:09

Angew is no longer proud of SO