Question: Modify the SearchableVector class template presented in this chapter so that it per- forms a binary search instead of a linear search. Test the template

Modify the SearchableVector class template presented in this chapter so that it per- forms a binary search instead of a linear search. Test the template in a driver program.

#ifndef SEARCHABLEVECTOR_H #define SEARCHABLEVECTOR_H #include "SimpleVector.h" template class SearchableVector : public SimpleVector { public: // Default constructor SearchableVector() : SimpleVector() { } // Constructor SearchableVector(int size) : SimpleVector(size) { } // Copy constructor SearchableVector(const SearchableVector &); // Accessor to find an item int findItem(const T); }; //******************************************************* // Copy constructor * //******************************************************* template SearchableVector::SearchableVector(const SearchableVector &obj) : SimpleVector(obj.size()) { for(int count = 0; count < this->size(); count++) this->operator[](count) = obj[count]; } //******************************************************** // findItem function * // This function searches for item. If item is found * // the subscript is returned. Otherwise 1 is returned. * //******************************************************** template int SearchableVector::findItem(const T item) { for (int count = 0; count <= this->size(); count++)16.4 Class Templates 1003 { if (getElementAt(count) == item) return count; } return 1; } #endif

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!