In: Computer Science
can someone finish and check my code on main. cpp? Its not working for me even though im sure my code make sense is it possible to output each function to show they work. this is supposed to be a vector class library made from allocated memory i have included templated functions in the class file to help create the rest of the functions. Thank you so much
note: i did not include main.cpp because it was empty- im hoping someone can test my vector class functions on int main. thank you
#ifndef VECTOR_H
#define VECTOR_H
#include
#include
#include
using namespace std;
template
T* allocate(int n){
return new T[n];
}
template
void copy_list(T *dest, T* src, int many_to_copy){
for (int i=0; i < many_to_copy; i++){
*dest++ = *src++;//destination pointer points to source pointer
}
*dest= NULL;
dest -=many_to_copy;
src -= many_to_copy;
delete [] src;//deletes src array
src = nullptr;
}
template
void print_list(T* list, int size){
cout<< "Content of list is:\n"<
for(int i = 0; i < size; i++){//loops "size" times
cout<< setw(4)<< *list<
list++; //list array increments
}
list = list-size;
cout<< endl;
}
// adds element to end of list
template
T* push_back(T* list,T entry, int& size){
T* p =list;// ponts to beginning of list
T* pnew = allocate(size+1);
for(int i=0; i
*pnew=*p;
pnew++= p++;
}
*pnew = entry;
size++; // old size gets increased by one
pnew -= size-1;
delete list;
list =nullptr;
return pnew;
}
template
T* pop_back(T* list, int& size){
size--;
T* p = &list;
T* pnew = allocate(size);// listnew points to dynamic array
for(int i=0;i
pnew=p;
pnew++;
p++;
}
pnew-= size-1;
delete list;
list = nullptr;
return pnew;
}
template
T* reallocate(T* arr, int& currentcapacity, int ncapacity){
T *pointer = allocate(ncapacity);
copy_list(pointer,arr,currentcapacity);
currentcapacity = ncapacity;
return pointer;
}
template
T* add_entry(T* list, const T& new_entry, int& size, int& capacity){
list = push_back(list,new_entry,size);
if(size == capacity) {
cout<<"Increasing capacity "<
list = reallocate(list,capacity,capacity*2);
}
return list;
}
template
T* search_entry(T* list, const T& find_me, int size) {
for(int i=0;i
if(*list == find_me) {
return list;
}
list++;
}
if(*list != find_me) {
cout<<"Not found!"<
list-=size;
print_list(list,size);
list = nullptr;
return list;
}
}
template
T* remove_entry(T* list, const T& delete_me, int& size, int& capacity) {
T * pointernew = search_entry(list,delete_me,size);
list = pop_back( list, pointernew, size);
if(size/4 == capacity) {
cout<<"Reducing capacity "<
list = reallocate(list,capacity,capacity/2);
}
return list;
}
class Vector
{
public:
Vector();
Vector(unsigned int size = 100);
// big three:
//big three functions:
Vector(const Vector& to_be_copied) //copy ctor: no return type
{
_how_many = 100;
_capacity= 100;
list =new int [_capacity+1];
}
Vector& operator=(const Vector& rhs) //assignment operator
{
const bool debug = true;
if (debug){
cout<<". . . . ["<list<<"].operator("<
}
if (this == &rhs){
return *this;
}
}
~Vector();
//member access functions:
template
const T operator [](const unsigned int index) const{//
assert(index < length());
return at(index);
}
template
T& operator [](const unsigned int index){
assert(index < length());
return at(index);
}
template
T& at(int index) //returns reference to item at position index
{
assert(index < length());
return list[index];
}
template
const T at(int index) const //returns a const item at position index
{
assert(index < length());
return list[index];
}
template
T& front() const //returns item at position 0.
{
* list-=_how_many-1;
return *list;
}
template
T& back() const //return item at the last position
{
* list+=_how_many;
return *list;
}
//Push and Pop functions:
template
Vector& operator +=(const T& item){} //push_back
template
void push_back(const T& item); //append to the end
template
T pop_back(); //remove last item and return it
//Insert and Erase:
template
void insert(int insert_here, const T& insert_this);//insert at pos
void erase(int erase_index);//erase item at position
template
int index_of(const T& item); //search for item. retur index.
//size and capacity:
void set_size(int size); //enlarge the vector to this size
//2mm
void set(const int* old_list);
void set_capacity(int capacity); //allocate this space
int size() const {return _how_many;} //return _size
int capacity() const {return _capacity;} //return _capacity
bool empty() const; //return true if vector is empty
int check_error() const;
//OUTPUT:
template
friend ostream& operator <<( ostream& outs, const Vector& _a );
template
void print_list(T* list, int size);
private:
int _how_many;//size
int _capacity;//cap
int _error;//errors
int* list;
int vector_len(const int* list) const;
int length() const;//reutrns vectorlength function
template
void v_cpy(T* dest, const T* src) const;
};
#endif // VECTOR_H
//vector.cpp
#include "vector.h"
//CTORS
Vector::Vector(){
_error =0;
list = nullptr;
set(list);
}
Vector::Vector(unsigned int _size){//defaultcotr
_size =100;
list = nullptr;
set(list);
}
Vector::~Vector(){//destructor
const bool debug = false;
if (debug){
cout<<". . . . Vector::~Vecotr being called."<
}
if (list!=nullptr)
delete[] list;
}
template
void Vector::push_back(const T& item){
}
//Insert and Erase:
template
void Vector::insert(int insert_here, const T& insert_this){}//inserts item from list
void Vector::erase(int erase_index){}// erease item from list
template
int Vector::index_of(const T& item){}//search array and return index
void Vector::set_size(int size){_how_many=size;}//returns size
void Vector::set_capacity(int capacity){_capacity= capacity;}
//returns length of list
int Vector::vector_len(const int* list) const{
const bool debug = false;
if (debug) cout<<". . . . Vector::vector_len ("<
int length = 0;
while(*list){
length++;
list++;
}
if (debug) cout<
return length;
}
int Vector::length() const{
return vector_len(list);
}
template
void Vector:: v_cpy(T* dest, const T* src) const{
while (src){
*dest++ = *src++;//destination pointer points to source pointer
}
*dest= NULL;
//dest -=many_to_copy;
//src -= many_to_copy;
delete [] src;//deletes src array
src = nullptr;
}
void Vector::set(const int* old_list){
if (list)
delete[] list;
int len = vector_len(old_list);
//this should be an allocate function.
list = new int [len+1];
v_cpy(list, old_list);
}
#include<bits/stdc++.h>
#ifndef VECTOR_H
#define VECTOR_H
using namespace std;
template
T* allocate(int n){
return new T[n];
}
template
void copy_list(T *dest, T* src, int many_to_copy){
for (int i=0; i < many_to_copy; i++){
*dest++ = *src++;//destination pointer points to source
pointer
}
*dest= NULL;
dest -=many_to_copy;
src -= many_to_copy;
delete [] src;//deletes src array
src = nullptr;
}
template
void print_list(T* list, int size){
cout<< "Content of list is:\n"<
for(int i = 0; i < size; i++){//loops "size" times
cout<< setw(4)<< *list<
list++; //list array increments
}
list = list-size;
cout<< endl;
}
// adds element to end of list
template
T* push_back(T* list,T entry, int& size){
T* p =list;// ponts to beginning of list
T* pnew = allocate(size+1);
for(int i=0; i
*pnew=*p;
pnew++= p++;
}
*pnew = entry;
size++; // old size gets increased by one
pnew -= size-1;
delete list;
list =nullptr;
return pnew;
}
template
T* pop_back(T* list, int& size){
size--;
T* p = &list;
T* pnew = allocate(size);// listnew points to dynamic array
for(int i=0;i
pnew=p;
pnew++;
p++;
}
pnew-= size-1;
delete list;
list = nullptr;
return pnew;
}
template
T* reallocate(T* arr, int& currentcapacity, int
ncapacity){
T *pointer = allocate(ncapacity);
copy_list(pointer,arr,currentcapacity);
currentcapacity = ncapacity;
return pointer;
}
template
T* add_entry(T* list, const T& new_entry, int& size,
int& capacity){
list = push_back(list,new_entry,size);
if(size == capacity) {
cout<<"Increasing capacity "<
list = reallocate(list,capacity,capacity*2);
}
return list;
}
template
T* search_entry(T* list, const T& find_me, int size) {
for(int i=0;i
if(*list == find_me) {
return list;
}
list++;
}
if(*list != find_me) {
cout<<"Not found!"<
list-=size;
print_list(list,size);
list = nullptr;
return list;
}
}
template
T* remove_entry(T* list, const T& delete_me, int& size,
int& capacity) {
T * pointernew = search_entry(list,delete_me,size);
list = pop_back( list, pointernew, size);
if(size/4 == capacity) {
cout<<"Reducing capacity "<
list = reallocate(list,capacity,capacity/2);
}
return list;
}
class Vector
{
public:
Vector();
Vector(unsigned int size = 100);
// big three:
//big three functions:
Vector(const Vector& to_be_copied) //copy ctor: no return
type
{
_how_many = 100;
_capacity= 100;
list =new int [_capacity+1];
}
Vector& operator=(const Vector& rhs) //assignment
operator
{
const bool debug = true;
if (debug){
cout<<". . . . ["<list<<"].operator("<
}
if (this == &rhs){
return *this;
}
}
~Vector();
//member access functions:
template
const T operator [](const unsigned int index) const{//
assert(index < length());
return at(index);
}
template
T& operator [](const unsigned int index){
assert(index < length());
return at(index);
}
template
T& at(int index) //returns reference to item at position
index
{
assert(index < length());
return list[index];
}
template
const T at(int index) const //returns a const item at position
index
{
assert(index < length());
return list[index];
}
template
T& front() const //returns item at position 0.
{
* list-=_how_many-1;
return *list;
}
template
T& back() const //return item at the last position
{
* list+=_how_many;
return *list;
}
//Push and Pop functions:
template
Vector& operator +=(const T& item){} //push_back
template
void push_back(const T& item); //append to the end
template
T pop_back(); //remove last item and return it
//Insert and Erase:
template
void insert(int insert_here, const T& insert_this);//insert at
pos
void erase(int erase_index);//erase item at position
template
int index_of(const T& item); //search for item. retur
index.
//size and capacity:
void set_size(int size); //enlarge the vector to this size
//2mm
void set(const int* old_list);
void set_capacity(int capacity); //allocate this space
int size() const {return _how_many;} //return _size
int capacity() const {return _capacity;} //return _capacity
bool empty() const; //return true if vector is empty
int check_error() const;
//OUTPUT:
template
friend ostream& operator <<( ostream& outs, const
Vector& _a );
template
void print_list(T* list, int size);
private:
int _how_many;//size
int _capacity;//cap
int _error;//errors
int* list;
int vector_len(const int* list) const;
int length() const;//reutrns vectorlength function
template
void v_cpy(T* dest, const T* src) const;
};
#endif // VECTOR_H
//vector.cpp
#include "vector.h"
//CTORS
Vector::Vector(){
_error =0;
list = nullptr;
set(list);
}
Vector::Vector(unsigned int _size){//defaultcotr
_size =100;
list = nullptr;
set(list);
}
Vector::~Vector(){//destructor
const bool debug = false;
if (debug){
cout<<". . . . Vector::~Vecotr being called."<
}
if (list!=nullptr)
delete[] list;
}
template
void Vector::push_back(const T& item){
}
//Insert and Erase:
template
void Vector::insert(int insert_here, const T&
insert_this){}//inserts item from list
void Vector::erase(int erase_index){}// erease item from list
template
int Vector::index_of(const T& item){}//search array and return
index
void Vector::set_size(int size){_how_many=size;}//returns
size
void Vector::set_capacity(int capacity){_capacity= capacity;}
//returns length of list
int Vector::vector_len(const int* list) const{
const bool debug = false;
if (debug) cout<<". . . . Vector::vector_len ("<
int length = 0;
while(*list){
length++;
list++;
}
if (debug) cout<
return length;
}
int Vector::length() const{
return vector_len(list);
}
template
void Vector:: v_cpy(T* dest, const T* src) const{
while (src){
*dest++ = *src++;//destination pointer points to source
pointer
}
*dest= NULL;
//dest -=many_to_copy;
//src -= many_to_copy;
delete [] src;//deletes src array
src = nullptr;
}
void Vector::set(const int* old_list){
if (list)
delete[] list;
int len = vector_len(old_list);
//this should be an allocate function.
list = new int [len+1];
v_cpy(list, old_list);
}