Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Create your own vector class which will test algorithms from Chapter 16 and those from the STL, Standard Template Library (Appendix H). Derive class myVector

Create your own vector class which will test algorithms from Chapter 16 and those from the STL, Standard Template Library (Appendix H).

Derive class myVector from vector. myVector must implement the following methods:

int seqSearch(T searchItem);

int binarySearch(T searchItem);

void bubbleSort();

void insertionSort();

Create a test program to create some vectors and test your methods above. Recall from your reading that binary search only works on a sorted list. Add a static member to the class to remember if the list is sorted ( i.e. binarySearch() should first sort the vector if its not sorted already).

Use the template below as a starter for your assignment. All comments in BOLD represent code which you need to implement:

#include

#include

#include

using namespace std;

template

class myVector: public vector {

public:

int seqSearch(T searchItem);

int binarySearch(T searchItem);

void bubbleSort();

void insertionSort();

};

template

int myVector::seqSearch(T searchItem)

{

//implement sequential search

}

template

void myVector::bubbleSort()

{

//implement bubble sort

}

template

void myVector::insertionSort()

{

//implement insertion sort

}

template

int myVector::binarySearch(T searchItem)

{

//implement binary search

}

int main()

{

//define test vector(s)

myVector nameList;

//add values to the vector(s)

//test sort methods

//test search methods

//print sorted vector using range based for loop

//define new test vector(s)

//define an iterator to each of the above vector containers

//add values to the vector(s)

//test the STL sort method

//test the STL binary_search algorithm

//print the resulting vector(s) using an iterator

return 0;

}

Useful notes:

this->size(); //length of vector from within myVector class

this->at(index); //value at specified index of vector from within myVector class

(Not sure if these photos will help but they are from Appendix H mentioned in the instructions)

image text in transcribedimage text in transcribedimage text in transcribed

Components of the STL The main objective of a program is to manipulate data and generate results. This requires the ability to store data in a computer memory, access a particular piece of data, and write algorithms to manipulate the data. For example, if all the data items are of the same type and we have some idea of the number of data items, we could use an array to store this data. We can then use the index to access a particular component of the array. Using a loop and the array index, we can step through the elements of the array. Algorithms, such as initializing the array, sorting, and searching, are used to manipulate the data stored in an array. On the other hand, if we do not want to be concerned with the size of the data, we can use a linked list to process the data. The STL is equipped with these and other features to effectively manipulate the data. More formally, the STL has the following three main components: - Containers - Iterators - Algorithms Containers and iterators are templatized classes. Iterators are used to step through the elements of a container. Algorithms are used to manipulate data. The ensuing sections discuss each of these three components in detail. The following example illustrates how to use a vector container in a program and how to process the elements in a vector container. Example H-4. Details Sample Run: Line 11: List elements: 13752835 Line 17: List elements: 261505670 Line 22: List elements: 261505670 Line 30: List elements: 26150885670 Sequence Container: Vectors A vector container stores and manages its objects in a dynamic array. Because an array is a random access data structure, the elements of a vector can be accessed randomly. Item insertion at the beginning or middle of an array is time consuming, especially if the array is large. However, inserting an item at the end is quite fast. Note Chapter 16 briefly introduced the class vector and explained how to declare a vector object and manipulate the data in that object. However, to make this appendix a standalone section, we repeat some of the material in Chapter 16 . We also provide a detailed discussion of the class vector. The name of the class that implements the vector container is vector. (Recall that containers are class templates.) The name of the header file containing the class vector is vector. Thus, to use a vector container in a program, the program must include the following statement: \#include Details Furthermore, to define an object of type vector, we must specify the type of the object because the class vector is a class template. For example, the statement vector intlist

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

More Books

Students also viewed these Databases questions

Question

Explain the Neolithic age compared to the paleolithic age ?

Answered: 1 week ago

Question

What is loss of bone density and strength as ?

Answered: 1 week ago