adding testing file

This commit is contained in:
Jidong Xiao
2023-09-26 12:40:19 -04:00
parent 6eb5eb10a1
commit 0ea801760a
2 changed files with 157 additions and 0 deletions

View File

@@ -0,0 +1,88 @@
#include <iostream>
//#include <vector>
#include "vec.h"
int main(){
// Vec is a template, not a type.
Vec<std::string> teams;
teams.push_back("rpi");
teams.push_back("wpi");
teams.push_back("yale");
teams.push_back("brown");
teams.push_back("cornell");
teams.push_back("colgate");
teams.push_back("miami");
teams.push_back("colorado");
teams.push_back("harvard");
// we can use a type alias defined inside a class even if there is no object of that class. The type alias becomes part of the class's scope and can be used anywhere in your code where the class's scope is visible. although no object of Vec is created in this code, the type alias size_type is still accessible because it is part of the class's scope.
for(Vec<int>::size_type i = 0; i < teams.size(); i++){
std::cout << teams[i] << std::endl;
}
std::cout<<"========="<<std::endl;
teams.pop_back();
for(Vec<int>::size_type i = 0; i < teams.size(); i++){
std::cout << teams[i] << std::endl;
}
std::cout<<"========="<<std::endl;
//teams.erase(2);
for(Vec<int>::size_type i = 0; i < teams.size(); i++){
std::cout << teams[i] << std::endl;
}
std::cout<<"========="<<std::endl;
// have 4 doubles in this vector, each double has 0.0 as its value.
Vec<double> v(4, 0.0);
v[0] = 13.1; v[2] = 3.14;
// copy a vector - calls copy constructor.
Vec<double> u(v);
u[2] = 6.5;
u[3] = -4.8;
for (unsigned int i = 0; i < v.size(); ++i){
std::cout << "u[" << i << "] is " << u[i] << " and v[" << i << "] is " << v[i] << std::endl;
}
std::cout<<"========="<<std::endl;
// equivalent to Vector<double> w(v), w is a copy of the elements in v.
// we use the const keyword in front of a variable definition to indicate that the value of the variable cannot be changed after it is initialized.
const Vec<double> w = v;
for (unsigned int i = 0; i < v.size(); ++i){
std::cout << "w[" << i << "] is " << w[i] << " and v[" << i << "] is " << v[i] << std::endl;
}
std::cout<<"========="<<std::endl;
v[3] = 2.6;
// they should now be different.
for (unsigned int i = 0; i < v.size(); ++i){
std::cout << "w[" << i << "] is " << w[i] << " and v[" << i << "] is " << v[i] << std::endl;
}
std::cout<<"========="<<std::endl;
Vec<double> y;
y = v;
for (unsigned int i = 0; i < v.size(); ++i){
std::cout << "y[" << i << "] is " << y[i] << " and v[" << i << "] is " << v[i] << std::endl;
}
std::cout<<"========="<<std::endl;
v[3] = 4.1;
// they should now be different.
for (unsigned int i = 0; i < v.size(); ++i){
std::cout << "y[" << i << "] is " << y[i] << " and v[" << i << "] is " << v[i] << std::endl;
}
std::cout<<"========="<<std::endl;
// two versions of the [] operator
double x;
x = w[2];
std::cout << "x is " << x << std::endl;
std::cout<<"========="<<std::endl;
// out of bound access.
u[6] = 2.2;
std::cout << "u[6] is " << u[6] << std::endl;
std::cout<<"========="<<std::endl;
//std::cout << "w[10] is " << w[10] << std::endl;
std::cout << "v[9] is " << v[9] << std::endl;
}

View File

@@ -0,0 +1,69 @@
template<class T>
class Vec{
public:
typedef unsigned int size_type;
Vec(){
m_data = new T[2];
m_size = 0;
capacity = 2;
}
Vec(int size, const T& val){
m_data = new T[size];
m_size = size;
capacity = size;
for(int i=0;i<size;i++){
m_data[i] = val;
}
}
Vec(const Vec& other){
capacity = other.capacity;
m_size = other.m_size;
m_data = new T[capacity];
for(unsigned int i=0;i<m_size;i++){
m_data[i] = other.m_data[i];
}
}
~Vec(){
delete [] m_data;
}
Vec<T>& operator=(const Vec& other){
if(this != &other){
capacity = other.capacity;
m_size = other.m_size;
m_data = new T[m_size];
for(unsigned int i=0;i<m_size;i++){
m_data[i] = other.m_data[i];
}
}
return *this;
}
T& operator[](int i){
return m_data[i];
}
const T& operator[](int i) const {
return m_data[i];
}
unsigned int size(){
return m_size;
}
void push_back(const T& val){
if(m_size >= capacity){
capacity = capacity * 2;
T* temp = new T[capacity];
for(unsigned int i=0;i<m_size;i++){
temp[i] = m_data[i];
}
delete [] m_data;
m_data = temp;
}
m_data[m_size] = val;
m_size++;
}
void pop_back(){
m_size--;
}
private:
int capacity;
int m_size;
T* m_data;
};