实现c++标准模板库STL(implement the c++ STL)
Author : yqtao
https://github.com/yqtaowhu
学c++同学必会的一项技能必须懂得STL,STL真的非常的强大,因此自己想实现一个自己的STL库,因此在读了《STL源码剖析》之后,开始了编写自己的库道路,如果你有兴趣,欢迎和我一起研究,完整的代码和测试案例在我的Github:https://github.com/yqtaowhu/DataStructureAndAlgorithm
vector的实现概要:
- 在实现vector的过程中,自己并没有编写自己的配置器(Allocator),而是用了c++标准的配置器,配置器的内容在c++标准头文件
<memory>
中。 - 定义vecotr的嵌套类型
typedef T value_type;
typedef value_type* iterator;
typedef const value_type* const_iterator;
typedef value_type& reference;
typedef value_type* pointer;
typedef size_t size_type;
typedef ptrdiff_t difference_type; //表示两个迭代器之间的距离 ,c++内置定义 typedef int ptrdiff_t;
- 三个重要的迭代器
iterator _start; //数组的首元素
iterator _end; //目前使用空间的尾
iterator _end_of_storage; //目前可用空间的尾
- vector中众多的构造函数,当然还有一个初始化列表没有实现,同时还有一些没有实现,因为太多了,自己也是实现了一部分。
myVector() :_start(0), _end(0), _end_of_storage(0){}//默认构造函数
myVector(size_type n, const T& value);
myVector(size_type n);
myVector(iterator first, iterator last);
myVector(const myVector& v); //复制构造函数
myVector& operator=(const myVector& rhs); //赋值操作符函数
- vector容器操作
iterator begin() { return _start; }
iterator end() { return _end; }
const_iterator cbegin() const { return _start; } //常量迭代器
const_iterator cend() const { return _end; }
size_type size() { return size_type(end() - begin()); } //注意转换成size_t类型
size_type capacity() { return size_type(_end_of_storage - begin()); }
bool empty() { return begin() == end(); }
void swap(myVector &other);
reference front() { return *begin(); }
reference back() { return *(end() - 1); }
reference operator[] (size_type n) { return *(begin() + n); } //重载[],这样可以用a[n]进行访问元素
//删除数组中的元素,并且释放内存
void insert_aux(iterator positon, const T& x); //一个插入辅助的函数,在向量为满的时候用到
void push_back(const T& value);
void pop_back();
void insert(iterator position, size_type n, const T& x);
iterator erase(iterator position);
iterator erase(iterator first, iterator last); //删除[first,last)的元素
void clear() { erase(begin(), end()); }
**下面是自己的测试结果,测试的样本并不多,因此肯定有Bug,如果有兴趣,欢迎指出。**
#include "myVector.h"
#include <string>
#include <vector>
using namespace std;
int main()
{
myVector<float>v;
cout << "测试默认构造函数" << endl;
cout << v.size() << " " << v.capacity() << endl;
cout << "测试构造函数,构造10个1元素" << endl;
myVector<int> v1(10, 1);
for (int i = 0; i < v1.size(); i++)
cout << v1[i] <<" "; //操作符重载[]成功
cout << endl;
cout << "得到头和尾的值" << endl;
cout <<v1.front()<<" "<<v1.back() << endl; //测试函数
cout << endl;
cout << "测试复制构造函数" << endl;
myVector<int> vcopy(v1);
for (int i = 0; i < vcopy.size(); i++)
cout << vcopy[i] <<" ";
cout << endl;
cout << "测试赋值操作符" << endl;
myVector<int>vv;
vv = v1;
for (int i = 0; i < vv.size(); i++)
cout << vv[i] << " ";
cout << endl;
cout << "测试构造函数,构造n个0元素" << endl;
myVector<float>v2(10);
for (int i = 0; i < v2.size(); i++)
cout << v2[i] << " ";
cout << "测试是否为空" << endl;
cout <<boolalpha<< v2.empty() << endl;
cout << "测试push_back()" << endl;
v2.push_back(3);
cout << v2.size() << " " << v2.capacity() << " " << v2[1] << endl;
cout << "测试pop_back()" << endl;
v2.pop_back();
cout << v2.size() << " " << v2.capacity() << " " << v2[10] << endl;
cout << "测试erase(fisrt,last)" << endl;
for (int i = 0; i < v2.size(); i++) {
v2[i] += i; cout << v2[i] << " ";
}
cout << endl;
v2.erase(v2.begin(), v2.begin() + 2); //从开始,删除两个元素
for (int i = 0; i < v2.size(); i++)
cout << v2[i] << " ";
cout << endl;
cout << "测试erase(positon)" << endl;
v2.erase(v2.begin() + 2);
for (int i = 0; i < v2.size(); i++)
cout << v2[i] << " ";
cout << endl;
//测试重要的函数insert()
cout << "测试insert(iterator positon, size_type n, const T& x)" << endl;
myVector<string>vec(5, "test");
for (int i = 0; i < vec.size(); i++)
cout << vec[i] << " " ;
cout << endl;
vec.insert(vec.begin() + 2, 7, "program"); //加入7个相同字符
for (int i = 0; i < vec.size(); i++)
cout << vec[i] << " ";
cout << endl;
cout << vec.size() << " " << vec.capacity() << endl;
cout << "测试迭代器" << endl;
myVector<string> str(10, "test");
auto it = str.begin(); //auto位c++11关键字,判断类型
for (; it != str.end(); ++it)
cout << *it << " ";
cout << endl;
cout << "使矩阵给容器赋初值" << endl;
int a[] = { 1,2,3,4,5,6,7,8,9 };
myVector<int> v4(a, a + sizeof(a)/sizeof(a[0]));
for (int i = 0; i < v4.size(); i++)
cout << v4[i] << " ";
cout << endl;
}