I have a vector and I need to copy it as a template vector class? So if I modify one element in the original vector it needs to change in the other too. Here is my main function
#include <iostream>
#include "thisheader.h"
#include <vector>
int main()
{
std::vector<int> v;
v.push_back(2);
v.push_back(4);
v.push_back(6);
std::vector<double> a;
a.push_back(4.32);
templatevectorclass<int> vv(v);
templatevectorclass<double> av(a);
v[0] = 3;
if(vv[0]==v[0]){
std::cout << "good" ;
}else{
std::cout << "bad";
}
a[0] = 2.4;
if(av[0]==a[0]){
std::cout << "good" ;
}else{
std::cout << "bad";
}
}
Here is the class that needs to copy the vector by reference:
#include <vector>
template<class T1>
class templatevectorclass {
std::vector<T1> copy;
T1* array;
public:
templatevectorclass(std::vector<T1> vector) {
for(int i = 0; i < vector.size(); i++)
{
copy.push_back(vector[i]);
}
}
T1 const& operator[](int index) const
{
return copy[index];
}
};