will_a
will_a

Reputation: 25

Can a hash table in c++ be used in a similar fashion to a dictionary in Python?

I'm in a grade 12 computer science class and my teacher asked us to do an assignment involving basic function review to get comfortable with C++ once again since last year.

I'm recreating a Python program that uses a dictionary to hold a key value as a store product (ex. "Pasta") and the value of that key as a price (ex. 3.99) and uses that dictionary to create a shopping experience, that fills a cart and then checks out the items within the cart using the keys and their values.

However, I struggle to recreate that key-value storage a dictionary provides within C++.

I was wondering if a hash table would be an efficient solution? I've done some research on it and it appears promising but I don't know if it is a dead end.

Upvotes: 0

Views: 663

Answers (1)

eesiraed
eesiraed

Reputation: 4654

The C++ standard library has a container called std::map that stores key-value pairs and can quickly find the value given the key. It is implemented as a binary search tree so it searching has a logarithmic complexity.

There is also a container called std::unordered_map which uses a hash table to store the elements with a constant average searching complexity.

Upvotes: 1

Related Questions