C++ Vector 模板类

内容来源学习

我的博客原文

Vector.h

#pragma once
#include <iostream>
using namespace std;
template <typename T>
class Vector
{
	//template <typename T>
	friend ostream &operator<<<T>(ostream &os, const Vector & object);//友原函数  可以直接访问对象的数据成员
public:
	Vector(int size);
	Vector(const Vector &cVector);
	Vector &operator=(const Vector &other);
	T &operator[](int index);
	int getLenght();
	~Vector(void);
private:
	int m_size;
	T *m_base;
};

Vector.cpp

#include "Vector.h"

template <typename T>
Vector<T>::Vector(int size)
{
	if (size>0)
	{
		m_size = size;
		m_base = new T[size];
	}
}

template <typename T>
Vector<T>::Vector(const Vector<T> &cVector){

	m_size = cVector.m_size;
	m_base = new T[m_size];

	for (int i=0; i <m_size;i++)
	{
		m_base[i] = cVector.m_base[i];
	}
}

template <typename T>
Vector<T>::~Vector(void)
{
	if (m_base!=NULL)
	{
		delete[] m_base;
		m_base = NULL;
		m_size = 0;
	}
}

template <typename T>
Vector<T> &Vector<T>::operator=(const Vector<T> &other){

	if (m_base!=NULL)
	{
		delete[] m_base;
		m_base = NULL;
		m_size = 0;
	}

	m_size = other.m_size;
	m_base = new T[m_size];
	for (int i = 0;i<m_size;i++)
	{
		m_base[i] = other.m_base[i];//不可以直接使用下标'[]' other[]
	}
	return *this;
}

template <typename T>
T &Vector<T>::operator[](int index)
{
	return m_base[index];
}

template <typename T>
int Vector<T>::getLenght(){
	return m_size;
}


template <typename T>
ostream &operator<<(ostream &os, const Vector<T> & object)
{
	for (int i=0;i<object.m_size;i++)
	{
		os<<object.m_base[i]<<" ";
	}
	os<<endl;
	return os;
}

main.cpp

#include <iostream>
#include "Vector.cpp"
#include "Student.h"
using namespace std;


int main(){
	Vector<int> v1(10);
	Vector<int> v2(10);

	for (int i=0; i<10 ;i++)
	{
		v1[i] = i;
	}

	for (int i=0;i<v1.getLenght();i++)
	{
		cout<<v1[i]<<" ";
	}
	cout<<endl;

	cout<<"-----------------------v3(v1)---------------"<<endl;
	Vector<int> v3(v1);

	for (int i=0;i<v3.getLenght();i++)
	{
		cout<<v3[i]<<" ";
	}
	cout<<endl;

	cout<<"-----------------------v3(v1)---end------------"<<endl;
	cout<<endl;
	cout<<"-----------------------v2=v1---------------"<<endl;
	v2 = v1;

	for (int i=0;i<v2.getLenght();i++)
	{
		cout<<v2[i]<<" ";
	}
	cout<<endl;

	cout<<"-----------------------v2=v1---end------------"<<endl;
	cout<<endl;
	cout<<"-----------------------float---------------"<<endl;
	Vector<float> v4(10);
	for (int i=0;i<v1.getLenght();i++)
	{
		v4[i] = v1[i]*0.1;
	}

	for (int i=0;i<v4.getLenght();i++)
	{
		cout<<v4[i]<<" ";
	}
	cout<<endl;

	cout<<"-----------------------float end---------------"<<endl;
	cout<<endl;
	cout<<"-----------------------<<---------------"<<endl;
	cout<<v1<<v4;
	cout<<"-----------------------<< end---------------"<<endl;
	cout<<endl;

	Student s1(18,"小明");
	Student s2(19,"小白");
	//s1.print();
	//s2.print();

	Vector<Student> vStudent(2);
	vStudent[0] = s1;//需要Student类存在默认构造函数
	vStudent[1] = s2;

	for (int i = 0;i<vStudent.getLenght();i++)
	{
		//vStudent[i].print();
		cout<<vStudent[i];
	}
	system("pause");
	return 0;
}

Student.h

#pragma once
#include <iostream>
using namespace std;
class Student
{
public:
	Student(void);
	Student(int _age,char *_name);
	~Student(void);

	void print();
	friend ostream &operator<<(ostream &os, const Student &s);
private:
	int age;
	char name[64];
};

Student.cpp

#include "Student.h"
#include <cstring>
#include <iostream>
using namespace std;


Student::Student(void)
{
	age = 0;
	name[0] = '\0';
}


Student::~Student(void)
{
}

Student::Student(int _age,char *_name)
{
	age = _age;
	strcpy_s(name,64,_name);
}

void Student::print()
{
	cout<<"name: "<<name<<"  age: "<<age<<endl;
}

ostream &operator<<(ostream &os, const Student &s)
{
	os<<"name: "<<s.name<<" age: "<<s.age<<endl;
	return os;
}

0 1 2 3 4 5 6 7 8 9
-----------------------v3(v1)---------------
0 1 2 3 4 5 6 7 8 9
-----------------------v3(v1)---end------------

-----------------------v2=v1---------------
0 1 2 3 4 5 6 7 8 9
-----------------------v2=v1---end------------

-----------------------float---------------
0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9
-----------------------float end---------------

-----------------------<<---------------
0 1 2 3 4 5 6 7 8 9
0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9
-----------------------<< end---------------

name: 小明 age: 18
name: 小白 age: 19
请按任意键继续. . .
 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值