C++第10课 STL容器 (一)
1.array
myArray.hpp
//定义和实现写在一起的时候,用hpp #include <iostream> using namespace std; template <class T, size_t _size> class MyArray { public: MyArray() { memory = new T[_size]; } T& operator[](int index) { return memory[index]; } public: T* begin() { return memory; } T* end() { return memory + _size; } T front() { return memory[0]; } T back() { return memory[_size - 1]; } T at(int index) { return memory[index]; } int size() { return _size; } class iterator { public: iterator operator=(T* pmove) { this->pMove = pmove; return *this; } bool operator!=(T* pmove) { return this->pMove != pmove; } iterator operator++(int) { this->pMove++; return (*this); } T operator*() { return pMove[0]; } protected: T* pMove; }; protected: T* memory; };
2.main.cpp
#include <iostream> #include <string> #include <array> #include "myArray.hpp" using namespace std; /* array:定长数组 1.array的基本操作 2.array操作自定义类型 3.简单模拟一下array的实现 */ //1.基本操作 void testCreateArray() { array<double, 2> a; array<int, 3> arr = { 1,2,3 }; } void useArray() { array<string, 3> arr; arr[0] = "Hello"; arr[1] = "Hi"; arr[2] = "!"; for (string str : arr) { cout << str << "\t"; } cout << endl; for (int i = 0; i < arr.size(); i++) { cout << arr[i] << "\t"; } cout << endl; //内置迭代器遍历 array<string, 3>::iterator iter; for (iter = arr.begin(); iter != arr.end(); iter++) { cout << *iter << "\t"; } cout << endl; //at方法 for (int i = 0; i < arr.size(); i++) { cout <<arr.at(i) << "\t"; } cout << endl; } void arrayFunc() { array<int, 3> Num = { 1,2,3 }; cout << Num.size() << endl; cout << Num.empty() << endl; cout << Num.front() << endl; cout << Num.at(0) << endl; cout << Num.back() << endl; int iNum[3] = { 1,2,3 }; //assign 在 c + + 11 中已过时,替换为 fill 。 替换所有元素。 array<int, 3> Data; Data.fill(3); for (auto v : Data) { cout << v << "\t"; } cout << endl; } //类对象 就是一个数据,所以数据可以做的它都可以 //当做函数参数 //当做函数返回值 //当做类的数据成员也是可以 template <class T, unsigned int size> void print(array<T, size>& str) { for (auto v : str) { cout << v << "\t"; } cout << endl; } void testParamer() { array<string, 3> str = { "ABC","CAD","DGG" }; //显示调用 print<string, 3>(str); //隐式调用 print(str); } //2.操作自定义, 无非就是重载 class MM { public: MM() {}; MM(string name, int age) :name(name), age(age) {} string getName() { return name; } int getAge() { return age; } friend ostream& operator<<(ostream& out, const MM& object); protected: string name; int age; }; ostream& operator<<(ostream& out, const MM& object) { out << object.name << "\t" << object.age <<endl; return out; } void arraySaveUserData() { array<MM, 3> mmData; mmData[0] = MM("mm1", 18); mmData[1] = MM("mm2", 20); mmData[2] = MM("mm3", 30); print<MM, 3>(mmData); cout << endl; for (auto v : mmData) { cout << v.getName() << "\t" << v.getAge() << endl; } for (int i = 0; i < mmData.size(); i++) { cout << mmData[i].getName() << "\t" << mmData[i].getAge() << endl; } } void testMyArray() { MyArray<string, 3> strData; //直接当做数组使用 strData[0] = "ILoveyou"; strData[1] = "Imiisyou"; //strData[3] = "IMiis"; 越界 strData[2] = "IMiis"; for (int i = 0; i < strData.size(); i++) { cout << strData[i] << "\t"; } cout << endl; //新的for循环 for (auto v : strData) { cout << v << "\t"; } cout << endl; //内置迭代器遍历 MyArray<string, 3>::iterator iter; for (iter = strData.begin(); iter != strData.end(); iter++) { cout << *iter << "\t"; } }