Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Some functions are not implemented in vector.h file. You need to implement those methods. For detailed guidelines, see the To do comments inside vector.h. Another

Some functions are not implemented in vector.h file. You need to implement those methods. For detailed guidelines, see the To do comments inside vector.h. Another file named myvector.cpp contains the main function. You can test your vector template using this file.

#ifndef VECTOR_H_

#define VECTOR_H_

#include // std::cout

#include // std::max

namespace ubcse{

template

class vector{

private:

/* Data fields */

/** Initial capacity of the array */

static const size_t INITIAL_CAPACITY; // size_t is equivalent to unsigned integers

// it starts from 0 .

/** The current capacity of the array */

size_t current_capacity;

/** The current number of items in the array */

size_t num_items;

/** The array to contain data .

* we acquire it in the heap with new command

*/

Item_Type* data_array;

public:

/*** Constructors and assignment operators ***/

// TO DO: implement default constructor.

/** Default constructor:

* it constructs an empty vector with default initial capacity.

* set num_items to zero

* set current_capacity to INITIAL_CAPACITY

*/

vector(){

// TO DO: put your code here for default constructor.

}

/** TO DO : implement Fill constructor

* This constructor has two arguments first one is 'n' and it means

* number of items. Second argument is a reference of type Item_Type named

* 'value'. You have to fill a vector with that 'n' number of 'value'.

* for example: vector char_vector(5,'b') will create a vector

* ['b','b','b','b','b']

* Your current capacity must be atleast INITIAL_CAPACITY.

*/

vector(size_t n, const Item_Type &value){

// TO DO : put your code here (for fill constructor)

}

/** TO DO: implement Range constructor

* Constructs a container with as many elements as the range [first,last],

* with each element constructed from

* its corresponding element in that range, in the same order.

* you can call the function as ubcse::vector v1(v2[2], v2[12]);

*/

vector(Item_Type& start,Item_Type& end){

//TO DO : put your code here (for range constructor)

}

/** TO DO: Slice constructor :

* This constructor takes three arguments as input, first one is a reference to

* the head of another vector. second one is the starting index and the third one is

* end index. You have slice the given array from start to end index to initialize the

* new vector with that slice.

*/

vector(const vector&other, int start, int end){

// TO DO: put your code here (for slice constructor)

}

/** TO DO: Implementation: Copy constructor

* It must perform deep copy

*/

vector(const vector&other) {

// TO DO: put your code for copy constructor here.

}

/** TO DO: Destructor: */

~vector(){

// put your code here (for destructor)

}

// Copy assignment.

vector& operator=(const vector&other);

// Get reference to element at index.

Item_Type& at(const int index);

// Get reference to element at index using [] op

Item_Type& operator[](const int& index);

// Get the number of elements stored.

size_t size() const;

// Return if the vector is storing values.

bool empty() const;

// reserve space for vector.

void reserve(size_t newSize);

// Resize size of storage for vector.

void resize(const size_t& newSize);

// Insert copy of value at index.

void insert(const int& index,const Item_Type& value);

// Insert copy of value at end of vector.

void push_back(const Item_Type& value);

// Erase element at index.

void erase(int index);

// print array

void print();

// concatanate two vectors.

void concat(const vector&other);

// reduce current capacity to num_items.

void shrink_to_fit();

}; //end of class vector

template

const size_t vector::INITIAL_CAPACITY = 10;

/** Implementation: assignment operator

*

*/

template

vector& vector::operator=(const vector& other) {

// This operator is used to copy the state of the

// other object into this instance of the object.

if(this != &other) {

if(current_capacity != other.current_capacity) {

// We need to reallocate storage size.

delete[] data_array;

// In case memory allocation fails,

// we update state of this object first.

// Allocate new space.

data_array = new Item_Type[other.current_capacity];

// Set appropriate size of stored values.

current_capacity = other.current_capacity;

}

// Copy memory over.

std::copy(other.data_array,other.data_array+other.num_items,data_array);

num_items = other.num_items;

}

return *this;

}

// Get reference to element at index.

template

Item_Type& vector::at(const int index) {

if(index >= current_capacity || index < 0) {

throw ;

}

return data_array[index];

}

// Get reference to element at index.

template

Item_Type& vector::operator[](const int& index) {

return this->at(index);

}

// Get the size of the vector.

template

size_t vector::size() const {

return num_items;

}

// Return if the vector is storing values.

template

bool vector::empty() const {

return num_items == 0;

}

// Reserve storage.

template

void vector::reserve(size_t newSize) {

if(current_capacity >= newSize) {

return;

}

Item_Type* temp = new Item_Type[newSize];

std::copy(data_array,data_array+num_items, temp);

std::swap(data_array,temp);

delete[] temp;

current_capacity = newSize;

}

// Resize vector.

template

void vector::resize(const size_t& newSize) {

if(current_capacity >= newSize) {

num_items = newSize > 0 ? newSize : 0;

}

else {

this->reserve(newSize);

num_items = newSize;

}

}

// Insert copy of value at index.

template

void vector::insert(const int& index,const Item_Type& value) {

if(index < 0 || index > num_items) {

throw;

}

// if we have maxed the size, double the capacity

size_t init = 1;

if(num_items >= current_capacity) {

this->reserve( std::max(init,current_capacity*2));

}

// shift elements right to make room for value.

for(int i = num_items - 1; i >= index; --i) {

data_array[i+1] = data_array[i];

}

data_array[index] = value;

num_items++;

}

// Insert value at end of storage.

template

void vector::push_back(const Item_Type& value) {

this->insert(num_items,value);

}

// Erase element at index.

template

void vector::erase(int index) {

if(index < 0 || index >= num_items) {

throw;

}

// shift elements left to cover gap.

for(int i = index+1; i < num_items; ++i) {

data_array[i-1] = data_array[i];

}

num_items--;

}

//TO DO: print the array:

template

void vector::print(){

// put your code here (for print)

}

/** TO DO: Implement concat:

* concat function actually concatanate two vectors.

* for Example : v1 = [1,2,3,4]

* v2 = [5,6,7,8]

* v1.concat(v2)

* after this operation v1 should look like: v1 = [1,2,3,4,5,6,7,8]

*/

template

void vector::concat(const vector&other){

// TO DO: write your code for concat method here.

}

// TO DO : perform the same concat operation by overloading the '+' operator.

// put your code here for '+' operator overloading

/* TO DO: implement the shrink_to_fit() method

* Requests the container to reduce its capacity to fit its size.

*/

// Put your code here (for hrink_to_fit() method)

} /* End of namespace ubcse */

#endif /* VECTOR_H_ */

================================================================

#include

#include "vector.h"

using namespace std;

int main() {

std::cout << "Create CSEVector: " << std::endl;

// These both call the default constructor.

ubcse::vector array1;

array1.insert(0,5);

if (true) {

std::cout << "Create array2 in a temp scope: " << std::endl;

ubcse::vector array2;

} // array2 scope exited.

std::cout << "Create a vector that is a copy of array: " << std::endl;

// This calls the copy constructor.

ubcse::vector array3 = array1;

array3.push_back(20);

array3.push_back(50);

array3.insert(0,100);

std::cout << " Contents of array1: ";

for(int i = 0; i < array1.size(); ++i) {

std::cout << array1[i] << " ";

}

std::cout << " Contents of array3: ";

for(int i = 0; i < array3.size(); ++i) {

std::cout << array3[i] << " ";

}

std::cout<< "check range constructor"<< std::endl;

ubcse::vector array4(array3[1],array3[2]);

std::cout << " Contents of array4: ";

for(int i = 0; i < array4.size(); ++i) {

std::cout << array4[i] << " ";

}

std::cout<< "check fill constructor"<< std::endl;

ubcse::vector array5(5,4);

std::cout << " Contents of array5: ";

array5.print();

//check copy constructor

ubcse::vector array6(array5);

std::cout << " Contents of array6: ";

array6.print();

array6.insert(3,200);

std::cout << " Contents of array6: ";

array6.print();

std::cout << " array6 is empty:"<< array6.empty()<

ubcse::vector array7(array3,1,3);

std::cout << " Contents of array7: ";

array7.print();

array6.concat(array7);

std::cout << " Contents of array6: ";

array6.print();

return 0;

return 0;

}

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

Database Application Development And Design

Authors: Michael V. Mannino

1st Edition

0072463678, 978-0072463675

More Books

Students also viewed these Databases questions

Question

Provide examples of KPIs in Human Capital Management.

Answered: 1 week ago

Question

What are OLAP Cubes?

Answered: 1 week ago