C++ Class Template for vector operations.
//Class Template for vector operations.
#include
#include
template
class vector
{
T *arr;
int size;
public:
vector() {
arr=NULL;
size=0;
}
vector(int m);
vector(T *a,int n);
void modify(T value,int index);
void multiply(int scalarvalue);
void display();
};
template
vector
{
size=m;
arr = new T[size];
for(int i=0;i
}
template
vector
{
size=n;
arr = new T[size];
for(int i=0;i
}
template
void vector
{
arr[index]=value;
}
template
void vector
{
for(int i=0;i
}
template
void vector
{
cout<<"(";
for(int i=0;i
cout<
cout<<", ";
}
cout<<")";
}
void main()
{
clrscr();
//Creating Integer Vector.
int iarr[]={1,2,3,4,5};
vector
cout<<"\nInteger Vector : ";
v1.display();
cout<<"\n\nModify index 3 with value 15\n";
v1.modify(15,3); //modifying index 3 with value 15.
cout<<"After Modification : ";
v1.display();
cout<<"\n\nMultiply with scalar value : 10\n";
v1.multiply(10); //Multiply with scalar value 10.
cout<<"After Multiplying : ";
v1.display();
cout<<"\n\n";
//Creating double Vector.
double darr[]={1.1,2.2,3.3,4.4,5.5};
vector
cout<<"\nDouble Vector : ";
v2.display();
cout<<"\n\nModify index 0 with value 9.9 \n";
v2.modify(9.9,0); //modifying index 0 with value 9.9.
cout<<"After Modification : ";
v2.display();
cout<<"\n\nMultiply with scalar value : 10\n";
v2.multiply(10); //Multiply with scalar value 10.
cout<<"After Multiplying : ";
v2.display();
cout<<"\n\n";
getch();
}