【C++】STL之vector类源码剖析

目录

概述

源码

MyVector.h

test.cpp

测试结果


概述

vector被称为动态数组,其所占内存根据数据大小而实时更新。普通数组只能实现一对一的映射,而vector可以实现一对多的映射。vector容器可以存储系统变量和自定义变量,也可以存储其他STL容器

优缺点:尾增尾删方便,头插头删时间复杂度大

算法设计:通过迭代器实现数据的增删,通过swap函数实现类的构造,简化代码

源码

MyVector.h

#include 
#include 
#include 

template
class Vector
{
public:
	// 迭代器
	typedef T* iterator;
	typedef const T* const_iterator;
	iterator begin()
	{
		return _start;
	}
	iterator end()
	{
		return _finish;
	}
	const iterator begin()const
	{
		return _start;
	}
	const iterator end()const
	{
		return _finish;
	}

	// 随机访问
	T& operator[](size_t pos)
	{
		assert(pos < size());
		return _start[pos];
	}
	const T& operator[](size_t pos)const
	{
		assert(pos < size());
		return _start[pos];
	}

	// 迭代器拷贝构造
	template
	Vector(InputIterator first, InputIterator last)
		: _start(nullptr), _finish(nullptr), _endofstorage(nullptr)
	{
		while (first != last)
		{
			push_back(*first);
			++first;
		}
	}

	void swap(Vector& t)
	{
		std::swap(_start, t._start);
		std::swap(_finish, t._finish);
		std::swap(_endofstorage, t._endofstorage);
	}
	Vector()
		: _start(nullptr), _finish(nullptr), _endofstorage(nullptr)
	{}
	Vector(const Vector& v)
		:_start(nullptr), _finish(nullptr), _endofstorage(nullptr)
	{
		Vector tmp(v.begin(), v.end());
		swap(tmp);
	}
	Vector(int size, T val = T())
		: _start(nullptr), _finish(nullptr), _endofstorage(nullptr)
	{
		reserve(size);
		for (int i = 0; i < size; ++i)
		{
			push_back(val);
		}
	}
	Vector& operator=(Vector v)
	{
		swap(v);
		return *this;
	}

	~Vector()
	{
		delete[] _start;
		_start = _finish = _endofstorage = nullptr;
	}

	void reserve(size_t n)
	{
		if (n > capacity())
		{
			size_t old_size = size();
			T* tmp = new T[n];
			if (_start)
			{
				for (size_t i = 0; i < old_size; ++i)
				{
					tmp[i] = _start[i];		//深拷贝
				}
				delete[] _start;
			}
			_start = tmp;
			_finish = _start + old_size;
			_endofstorage = _start + n;
		}
	}
	void resize(size_t n, T val = T())
	{
		if (n > capacity())
		{
			reserve(n);
		}
		if (n > size())
		{
			while (_finish < _start + n)
			{
				*_finish = val;
				++_finish;
			}
		}
		else
		{
			_finish = _start + n;
		}
	}

	bool empty()
	{
		return _finish == _start;
	}
	size_t size()const
	{
		return _finish - _start;
	}
	size_t capacity()const
	{
		return _endofstorage - _start;
	}

	void push_back(const T& x)
	{
		if (_finish == _endofstorage)
		{
			size_t newCapacity = (capacity() == 0 ? 4 : capacity() * 2);
			reserve(newCapacity);
		}
		*_finish = x;
		++_finish;
	}
	void pop_back()
	{
		assert(!empty());
		--_finish;
	}

	iterator insert(iterator pos, const T& val)
	{
		assert(pos >= _start && pos <= _finish);
		if (_finish == _endofstorage)
		{
			size_t newCapacity = (capacity() == 0 ? 4 : capacity() * 2);
			reserve(newCapacity);
			size_t len = pos - _start;
			pos = _start + len;		//防止迭代器失效
		}
		iterator end = _finish - 1;
		while (end >= pos)
		{
			*(end + 1) = *end;
			--end;
		}
		*pos = val;
		++_finish;
		return pos;
	}
	iterator erase(iterator pos)
	{
		assert(pos >= _start && pos < _finish);
		iterator begin = pos + 1;
		while (begin < _finish)
		{
			*(begin - 1) = *begin;
			++begin;
		}
		--_finish;
		return pos;
	}

	void clear()
	{
		_finish = _start;
	}

private:
	iterator _start;
	iterator _finish;
	iterator _endofstorage;
};

test.cpp

#include "MyVector.h"

void Test()
{
	Vector v;
	v.reserve(4);
	v.push_back(1);
	v.push_back(2);
	v.push_back(3);
	v.push_back(4);		// 1  2  3  4
	v[0] = 0;			// 0  2  3  4
	v.resize(10, -1);	// 0  2  3  4 -1 -1 -1 -1 -1 -1
	v.push_back(10);
	v.push_back(20);	// 0  2  3  4 -1 -1 -1 -1 -1 -1 10 20
	v.pop_back();		// 0  2  3  4 -1 -1 -1 -1 -1 -1 10
	v.insert(v.begin(), -2);	
	v.insert(v.end(), 30);	// -2  0  2  3  4 -1 -1 -1 -1 -1 -1 10 30
	Vector::iterator it = std::find(v.begin(), v.end(), 3);
	v.insert(it, 100);		// -2  0  2  100 3  4 -1 -1 -1 -1 -1 -1 10 30
	v.erase(it + 1);		// -2  0  2  100 4 -1 -1 -1 -1 -1 -1 10 30
	for (auto& e : v)
	{
		std::cout << e << " ";
	}
	std::cout << std::endl;

	Vector v1(v);
	for (auto& e : v)
	{
		std::cout << e << " ";
	}
	std::cout << std::endl;

	Vector v2 = v1;
	for (auto& e : v)
	{
		std::cout << e << " ";
	}
	std::cout << std::endl;

	Vector v3(10, 1);
	Vector> v4;
	v4.push_back(v3);
	v4.push_back(v3);
	v4.push_back(v3);
	Vector>::iterator it4 = v4.begin();
	while (it4 != v4.end())
	{
		Vector::iterator it3 = it4->begin();
		while (it3 != it4->end())
		{
			std::cout << *it3 << " ";
			++it3;
		}
		std::cout << std::endl;
		++it4;
	}
}

int main()
{
	try
	{
		Test();
	}
	catch (const std::exception& e)
	{
		std::cout << e.what() << std::endl;
	}
	return 0;
}

测试结果

【C++】STL之vector类源码剖析_第1张图片

你可能感兴趣的:(c++,STL,vector,数据结构)