简体   繁体   中英

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM