C++ Vector 模板类
内容来源学习使用的视频教程。
功能:实现Vector类,存储int ,float, double等类型元素,并实现存储自定义类Student.
重写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; }
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.ccp
#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; }
main.ccp
#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; }
输出结果:
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
请按任意键继续. . .
留言评论
暂无留言