implement your own version of vectors as an ADT to manage a collection of double
ID: 657883 • Letter: I
Question
implement your own version of vectors as an ADT to manage a collection of doubles using dynamic arrays. Specifically, your Vector_double ADT should include the following member functions:
a default constructor.
the "Big-3".
the "capacity" function: Returns the number of elements that the vector could store without allocating more storage.
the "empty" function: Tests if the vector container is empty.
the "erase" function: Removes an element or a range of elements in a vector from the specified positions.
the "insert" function: Inserts an element or a number of elements into the vector at a specified position.
the "pop_back" function: Deletes the element at the end of the vector.
the "push_back" function": Adds an element to the end of the vector.
the "resize" function: Specifies a new size for a vector. The new size can be smaller or larger than the old size.
the "shrink_to_fit" function: Discards excess capacity.
the "size" function" Returns the number of elements stored in the vector.
the overloaded output operator (<<) to print out the elements in a vector.
Explanation / Answer
#include <iostream>
#include<conio.h>
#include <vector>
using namespace std;
class Vectors
{
int n;
public:
Vectors()
{
cout<<"Default constructor is ";
cout<<"capacity function: ";
cpct();
cout<<"empty function: ";
emty();
cout<<"earse function ";
erse();
cout<<"insert function ";
insert();
cout<<"pop back function ";
popback();
cout<<"push back function ";
pushback();
cout<<"resize function ";
resz();
cout<<"shrink to fit function";
shrnk_to_fit();
cout<<"size function";
size();
}
void cpct()
{
std::vector<int> myvector;
for (int i=0; i<n; i++) myvector.push_back(i);
std::cout << "capacity: " << (int) myvector.capacity() << ' ';
std::cout << "max_size: or max_capacity " << (int) myvector.max_size() << ' ';
}
void emty()
{
int sum (0);
std::vector<int> myvector1;
for (int i=1;i<=10;i++) myvector1.push_back(i);
while (!myvector1.empty())
{
sum += myvector1.back();
myvector1.pop_back();
}
std::cout << "total: " << sum << ' ';
}
void erse()
{
std::vector<int> myvector2;
for (int i=1; i<=10; i++) myvector2.push_back(i);
myvector2.erase (myvector2.begin()+5);
myvector2.erase (myvector2.begin(),myvector2.begin()+3);
std::cout << "myvector contains:";
for (unsigned i=0; i<myvector2.size(); ++i)
std::cout << ' ' << myvector2[i];
std::cout << ' ';
}
void insert()
{
std::vector<int> myvector3 (3,100);
std::vector<int>::iterator it;
it = myvector3.begin();
it = myvector3.insert ( it , 200 );
myvector3.insert (it,2,300);
// "it" no longer valid, get a new one:
it = myvector3.begin();
std::vector<int> anothervector (2,400);
myvector3.insert (it+2,anothervector.begin(),anothervector.end());
int myarray [] = { 501,502,503 };
myvector3.insert (myvector3.begin(), myarray, myarray+3);
std::cout << "myvector contains:";
for (it=myvector3.begin(); it<myvector3.end(); it++)
std::cout << ' ' << *it;
std::cout << ' ';
}
void popback()
{
std::vector<int> myvector4;
int sum (0);
myvector4.push_back (100);
myvector4.push_back (200);
myvector4.push_back (300);
while (!myvector4.empty())
{
sum+=myvector4.back();
myvector4.pop_back();
}
std::cout << "The elements of myvector add up to " << sum << ' ';
}
void pushback()
{
std::vector<int> myvector5;
int myint;
std::cout << "Please enter some integers (enter 0 to end): ";
do {
std::cin >> myint;
myvector5.push_back (myint);
} while (myint);
std::cout << "myvector stores " << int(myvector5.size()) << " numbers. ";
}
void resz()
{
std::vector<int> myvector6;
// set some initial content:
for (int i=1;i<10;i++) myvector6.push_back(i);
myvector6.resize(5);
myvector6.resize(8,100);
myvector6.resize(12);
std::cout << "myvector contains:";
for (int i=0;i<myvector6.size();i++)
std::cout << ' ' << myvector6[i];
std::cout << ' ';
}
void shrnk_to_fit()
{
std::vector<int> myvector7 (100);
std::cout << "1. capacity of myvector: " << myvector7.capacity() << ' ';
myvector7.resize(10);
std::cout << "2. capacity of myvector: " << myvector7.capacity() << ' ';
myvector7.shrink_to_fit();
std::cout << "3. capacity of myvector: " << myvector7.capacity() << ' ';
}
void size()
{
std::vector<int> myints;
std::cout << "0. size: " << myints.size() << ' ';
for (int i=0; i<10; i++) myints.push_back(i);
std::cout << "1. size: " << myints.size() << ' ';
myints.insert (myints.end(),10,10);
std::cout << "2. size: " << myints.size() << ' ';
myints.pop_back();
std::cout << "3. size: " << myints.size() << ' ';
}
~Vectors()
{
cout<<"destructor";
}
};
int main ()
{
Vectors v;
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.