View the step-by-step solution to:

Problem 9 (5 points) A bidirectional map is a map which supports bidirectional lookup: given a key, you can find the corresponding value (as you can...

This question was created from ICS 46 Final Exam - Thornton


Question: Write the insert member function of the BidirectionalMap class (in C++) so that it properly supports the functionality of the BidirectionalMap, as described in the attachment. As a reminder, the method should take in the key-value pair, and insert the mapping into the BidirectionalMap (by updating the forward and back maps, as appropriate). Use only the two std::map attributes provided as data structures in your answer - you may use any std::map operation, such as find, count, at, erase, etc. template <typename K, typename V> void BidirectionalMap<K, V>::insert(K k, V v) {


Probiem 915 points[ A bidirectionoi mop is a map which supports bidirectional lookup: given a key, you can find the
corresponding value (as you can in a data type like std: :map), and given a value, find the
corresponding key. In a bidirectional map there is always a one-to-one relationship between keys and values. In other
words, each key has exactly one value, and each value is found under exactly one key. It supports the typical operations of insert, iookup, and erase. The new operation is the reverse iookup operation, which returns the key given the voiue of a potential
mapping. 0 rlookuplv): if the mapping contains a mapping k 9 v, return k. To implement a bidirectional map, we can use two maps internally as part of its implementation, as
follows, in a BidirectionalMap class. template &lt;typename K, typename V) class BidirectionalMap {
public: BidirectionalMap(); void insert(|( k, V v); // You implement this
‘v‘ lookup(l( k); void erase(K k); K rlookup(\i v); [I New operation private:
std::map&lt;K, V&gt; forward; // map from keys to values std::map&lt;|&lt;J V) back; [I map from values to keys
}; As you can see, theforword map keeps track of the standard mappings from keys to values, while hock
keeps track of the opposite. For a bidirectionoi map to be correct, these two maps must always be in sync, containing the same data throughout all insertions and removals from the BidirectionalMap. For this problem, assume that all functions are implemented correctly except for the insert member
function, which you will write on the next page. An insert operation into the BidirectionalMap should
take in the key-value pair, and insert the mapping into the BidirEctionalMap by updating its underlying
mapsforword and back, as appropriate). Note that any existing mapping with key k or value u should be removed first before insertion — we
don’t want inconsistencies or duplicates from this (Le. we should never have a key with multiple
values, and never have a value with multiple keys — note how this is different from a regular map.)

Recently Asked Questions

Why Join Course Hero?

Course Hero has all the homework and study help you need to succeed! We’ve got course-specific notes, study guides, and practice tests along with expert tutors.


Educational Resources
  • -

    Study Documents

    Find the best study resources around, tagged to your specific courses. Share your own to gain free Course Hero access.

    Browse Documents
  • -

    Question & Answers

    Get one-on-one homework help from our expert tutors—available online 24/7. Ask your own questions or browse existing Q&A threads. Satisfaction guaranteed!

    Ask a Question