实现非常简单的数组模板类(MyVector)中用到了泛型编程(模板类编程)、运算符重载、友元函数等知识,最重要的是加深了对内存分配的理解。
所有容器提供的都是值(value)语意,而非引用(reference)语意。容器执行插入元素的操作时,内部实施拷贝动作。所以STL容器内存储的元素必须能够被拷贝(必须提供拷贝构造函数)。
开始的时候不理解这句话,然后自己敲代码就理解了。我们在往容器里存数据的时候,是进行拷贝动作,也就是说将外部变量的值拷贝给容器中的值。要进行拷贝就必须分配内存,没有分配内存的话往哪拷数据呢????而基础数据类型的变量是不用担心这些问题的,因为当我们写下int a
时编译器已经为a
分配了内存,但是如果是指针变量就必须考虑深拷贝与浅拷贝的问题(其实这个地方第一次的时候我想的是,MyVector不是已经分配了内存了吗,为什么还要分配呢?其实在MyVector中是给类分配了内存说白了就是给类中的成员变量分配了内存,而成员变量若有指针的话,它只是为指针分配了内存,而我们所需要的是分配指针所指向的内存空间分配内存)。
MyVector.h
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
| #pragma once #include<iostream> using namespace std; template <typename Type> class MyVector { friend ostream& operator<< <Type> (ostream &out, const MyVector &obj); public: MyVector(int len); MyVector(const MyVector &obj); ~MyVector(); public: MyVector& operator=(const MyVector &obj); Type& operator[](int index); private: Type *mSpace; int len; };
|
MyVector.cpp
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62
| #include<iostream> using namespace std; #include"MyVector.h" template <typename Type> MyVector<Type>::MyVector(int len) { this->len = len; mSpace = new Type[len]; } template <typename Type> MyVector<Type>::MyVector(const MyVector &obj) { this->len = obj.len; mSpace = new Type[len]; for (int i = 0; i < len; i++) { mSpace[i] = obj.mSpace[i]; } } template <typename Type> MyVector<Type>::~MyVector() { if (mSpace != NULL) { delete[] mSpace; mSpace = NULL; len = 0; } } template <typename Type> MyVector<Type>& MyVector<Type>::operator=(const MyVector<Type> &obj) { if (mSpace != NULL) { delete[] mSpace; mSpace = NULL; len = 0; } this->len = obj.len; mSpace = new Type[len]; for (int i = 0; i < len; i++) { mSpace[i] = obj.mSpace[i]; } return *this; } template <typename Type> Type& MyVector<Type>::operator[](int index) { return mSpace[index]; } template <typename Type> ostream& operator<<(ostream &out, const MyVector<Type> &obj) { for (int i = 0; i < obj.len; i++) { cout << obj.mSpace[i]; } cout << endl; return out; }
|
main.cpp
测试int
型
1 2 3 4 5 6 7 8 9 10 11 12
| int main() { MyVector<int> arr1(10); for (int i = 0; i < 10; i++) { arr1[i] = i + 1; } cout << arr1 << endl; MyVector<int> arr2 = arr1; cout << arr2 << endl; return 0; }
|
测试char
型
1 2 3 4 5 6 7 8 9 10 11 12
| int main02() { MyVector<char> str1(10); for (int i = 0; i < 10; i++) { str1[i] = 'a' + i; } cout << str1 << endl; MyVector<char> str2 = str1; cout << str2 << endl; return 0; }
|
测试Teacher
类
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112
| class Teacher { friend ostream& operator<<(ostream& out, Teacher &obj); public: Teacher() { age = 0; strcpy(name, ""); } Teacher(const char *name,int age) { strcpy(this->name, name); this->age = age; } void printT() { cout << "name:" << name << " age:" << age << endl; } private: char name[16]; char *name; int age; }; ostream& operator<<(ostream& out, Teacher &obj) { out << obj.name << " " << obj.age << endl; return out; }
class Teacher { friend ostream& operator<<(ostream &out, Teacher &obj) { out << "name:" << obj.name << " age:" << obj.age << endl; return out; } public: Teacher(const char *name = "", int age = 0) { this->age = age; this->name = new char[strlen(name) + 1]; strcpy(this->name, name); } Teacher(const Teacher &obj) { this->age = obj.age; name = new char[strlen(obj.name) + 1]; strcpy(name, obj.name); } ~Teacher() { if (name != NULL) { delete[] name; name = NULL; age = 0; } } public: Teacher& operator=(const Teacher &obj) { this->age = obj.age; this->name = new char[strlen(obj.name) + 1]; strcpy(name, obj.name); return *this; } void printT() { cout << "name:" << name << " age:" << age << endl; } private: char *name; int age; }; int main() { MyVector<Teacher> tArray(3); Teacher t1("123", 30); Teacher t2("456", 31); Teacher t3("789", 32); tArray[0] = t1; tArray[1] = t2; tArray[2] = t3;
Teacher t4;
for (int i = 0; i < 3; i++) { Teacher tmp = tArray[i]; tmp.printT(); } cout << endl; cout << endl; cout << endl; cout << endl;
cout << tArray << endl; return 0; }
|