(COMP2012)[2014](s)final_=in8gpimcc^_56359.pdf

# Note that all the elements of the current map

• Test Prep
• 23

This preview shows pages 5–8. Sign up to view the full content.

, to the current map container. Note that all the elements of the current map container are erased. template <class K, class V> void Map<K, V>::copy(const Map& other){ if ((capacity != 0) && (elements!=NULL)) { delete[] elements; elements = NULL; } capacity = other.capacity; count = other.count; if( other.capacity == 0 ) return; elements = new KeyValuePair<K, V>[capacity]; for (int i=0; i<other.count; i++) elements[i] = other.elements[i]; // Or by the following two statements // elements[i].key = other.elements[i].key; // elements[i].value = other.elements[i].value; } (d) [5 marks] Implement the private member function doubleCapacity , which expands the container by doubling its capacity with a reallocation of storage space and elements, i.e., copying all the elements over to the new space. template <class K, class V> void Map<K, V>::doubleCapacity() { capacity*=2; KeyValuePair<K, V> *newArr = new KeyValuePair<K, V>[capacity]; for (int i=0; i<count; i++) newArr[i] = elements [i]; delete[]elements; elements = newArr; } (COMP2012)[2014](s)final~=in8gpimcc^_56359.pdf downloaded by xjiae from at 2018-03-13 17:27:35. Academic use within HKUST only.

This preview has intentionally blurred sections. Sign up to view the full version.

Page 6 of 23 (e) [5 marks] Implement the helper findKey function which returns the array index of the key-value pair corresponding to a given key value key . If the key does not exist, return -1. template <class K, class V> int Map<K, V>::findKey(const K& key) const { for (int i=0; i<count; i++) if (elements[i].key == key) return i; return -1; } (f) [6 marks] Implement the insert function, which inserts a key, called key , and a mapped value, called value , into the current map container. If the key already exists in the array, overwrite the mapped value; otherwise, add the key and value into the current map. If there is not enough capacity, expand the capacity of the array by invoking the doubleCapacity function. template <class K, class V> void Map<K, V>::insert(const K& key, const V& value) { if (isEmpty()){ // empty map capacity = 1; elements = new KeyValuePair< K, V >; elements[0].key = key; elements[0].value = value; count = 1; return; } int index = findKey(key); if (index == -1) { if (count == capacity) // not enough capacity to insert doubleCapacity(); index = count++; elements[index].key = key; } elements[index].value = value; } (COMP2012)[2014](s)final~=in8gpimcc^_56359.pdf downloaded by xjiae from at 2018-03-13 17:27:35. Academic use within HKUST only.
Page 7 of 23 (g) [3 marks] Implement operator+ which extends the container by inserting a given key-value pair called newPair into the current Map container. If the key already exists in the array, overwrite the mapped value; otherwise, add the key-value pair and double the capacity if necessary. The operator+ should support concatenation, i.e. a+b+c , where a is a Map object, b and c are KeyValuePair objects. template <class K, class V> Map<K, V> Map<K, V>::operator+(const KeyValuePair<K, V>& newPair) { insert(newPair.key, newPair.value); return (*this); } (h) [4 marks] Implement the two subscript operator functions which overloads [] by returning the mapped value given the key named key . You may assume that key always exists in the map in this problem.

This preview has intentionally blurred sections. Sign up to view the full version.

This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

### What students are saying

• As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

Kiran Temple University Fox School of Business ‘17, Course Hero Intern

• I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

Dana University of Pennsylvania ‘17, Course Hero Intern

• The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

Jill Tulane University ‘16, Course Hero Intern