模板编程学习注意点

最近刚开始学习模板编程,有些注意点
1.在类模板编程中对操作符<<进行重载时,类中声明时需要使用
friend ostream& operator<< <T>(ostream &out, const MyVector &obj);

2.所有容器提供的都是值(value)语意,而非引用(reference)语意。容器执行插入元素的操作时,内部实施拷贝动作。所以STL容器内存储的元素必须能够被拷贝(必须提供拷贝构造函数)
这个意思指在容器执行插入元素操作时,是把元素拷贝一份放在容器中,而不是引用对象。所以STL容器内存储的元素必须是能够被拷贝的(如果对象中存在指针类型的成员变量,则需要支持深拷贝)
下面是我写了一个简单的MyVector类模板,并且写了一个Teacher类,大家可以拷过去看看,应该会更加清楚
</pre><pre code_snippet_id="1562142" snippet_file_name="blog_20160122_1_5172763" name="code" class="html">
------------------myvector.h
#pragma once

#include<iostream>
using namespace std;

template<class T>
class MyVector
{
	friend ostream& operator<< <T>(ostream &out, const MyVector &obj);
public:
	MyVector(int size=0);
	MyVector(const MyVector& obj);
	~MyVector();
	T& operator[](int index);
	int GetVectorSize();
	MyVector& operator=(const MyVector &obj);

private:
	T *m_ptr;
	int size;
};
------------------myvector.hpp
<span style="font-size:14px;"></span><pre name="code" class="cpp">#include<iostream>
#include"demo9.h"
#include"teacher.h"


using namespace std;

template<typename T>
ostream& operator<< (ostream &out, const MyVector<T> &obj)
{
	for (size_t i = 0; i < obj.size; i++)
	{
		out << obj.m_ptr[i] << "   ";
	}
	out << endl;
	return out;
}

template<typename T>
MyVector<T>::MyVector(int size=0)
{
	this->size = size;
	m_ptr = new T[size];
}


template<typename T>
MyVector<T>::MyVector(const MyVector& obj)
{
	this->size = obj.size;
	this->m_ptr = new T[size];

	for (size_t i = 0; i < this->size; i++)         //不能用memcpy,如果T类型的复杂类型(包含指针)这需要支持深拷贝
	{
		this->m_ptr[i] = obj.m_ptr[i];                                 
	}	
}

template<typename T>
MyVector<T>& MyVector<T>::operator=(const MyVector<T>& obj)
{
	MyVector temp(obj);
	this->size = temp.size;
	swap(this->m_ptr, temp.m_ptr);

	return *this;
}

template<typename T>
MyVector<T>::~MyVector()
{
	if (m_ptr != NULL)
	{
		delete[] m_ptr;
		m_ptr = NULL;
	}
}
template<typename T>
T& MyVector<T>::operator[](int index)
{
	return m_ptr[index];
}


template<typename T>
int MyVector<T>::GetVectorSize()
{
	return size;
}

------------------teacher.h
<pre name="code" class="cpp">#pragma once
#include<iostream>
using namespace std;

class Teacher
{
	friend ostream & operator<< (ostream & out, const Teacher &obj);
	friend ostream & operator<<(ostream& out, const Teacher * obj);
public:
	Teacher();

	Teacher(const Teacher &obj);

	Teacher& operator=(const Teacher &obj);

	Teacher(char *name, int age);
	void printT();
private:
	int age;
	char *pname;
	
};

------------------teacher.cpp
 
<pre name="code" class="cpp">#include<iostream>
#include"teacher.h"
#include<string.h>

using namespace std;


Teacher::Teacher()
	{
		age = 33;
		pname = NULL;
	}

Teacher::Teacher(char *name, int age)
	{
		this->age = age;
		pname = new char[strlen(name) + 1];
		strcpy_s(pname, strlen(name) + 1, name);
	}

void Teacher::printT()
	{
		cout << pname << ", " << age << endl;
	}

ostream& operator<<(ostream& out, const Teacher &obj)
{
	return out << "age:" << obj.age << "name:" << obj.pname;
}


Teacher::Teacher(const Teacher &obj)
{
	this->age = obj.age;
	this->pname = new char[strlen(obj.pname) + 1];
	strcpy_s(this->pname, strlen(obj.pname) + 1, obj.pname);
}

Teacher& Teacher::operator=(const Teacher &obj)
{
	Teacher temp(obj);
	this->age = temp.age;
	swap(this->pname, temp.pname);

	return *this;
}

ostream & operator<<(ostream& out, const Teacher * obj)
{
	return cout << "age:" << obj->age << "name:" << obj->pname;
}

 
------------------main.cpp
#include<iostream>
#include"demo9.hpp"
#include"teacher.h"


void main()
{


Teacher t1("t1", 31), t2("t2", 32), t3("t3", 33), t4("t4", 34);


MyVector<Teacher> tArray(4);


tArray[0] = t1;
tArray[1] = t2;
tArray[2] = t3;
tArray[3] = t4;


MyVector<Teacher> *mArray = new MyVector<Teacher>;
*mArray = tArray;
MyVector<Teacher> nArray(*mArray);


for (int i = 0; i<4; i++)
{
Teacher tmp = tArray[i];
tmp.printT();
}
cout <<"tArray"<< tArray;


cout << "***************************" << endl;


cout << "mArray" << *mArray;



cout << "---------------------------" << endl;


cout << "nArray" << nArray;


delete mArray;




cout << "+++++++++++++++++++++++++++" << endl;
Teacher t5("xbb", 20), t6("sddf", 30), t7("dsd", 60);
MyVector<Teacher*> pArray(3);
pArray[0] = &t5;
pArray[1] = &t6;
pArray[2] = &t7;


cout << pArray << endl;




system("pause");
}


 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值