vector底层实现(源码)

 

#pragma once

#include<assert.h>

namespace bit

{

template<class T>

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;

}

vector()

{}

// v2(v1)

vector(const vector<T>& v)

{

reserve(v.capacity());

for (auto& e : v)

{

push_back(e);

}

}

// vector<int> v1 = { 1,2,3,4,5,6,7,8,9,10 };

vector(initializer_list<T> il)

{

reserve(il.size());

for (auto& e : il)

{

push_back(e);

}

}

// 类模板的成员函数可以是函数模板

template <class InputIterator>

vector(InputIterator first, InputIterator last)

{

while (first != last)

{

push_back(*first);

++first;

}

}

vector(size_t n, const T& val = T())

{

reserve(n);

for (size_t i = 0; i < n; i++)

{

push_back(val);

}

}

vector(int n, const T& val = T())

{

reserve(n);

for (int i = 0; i < n; i++)

{

push_back(val);

}

}

void swap(vector<T>& v)

{

std::swap(_start, v._start);

std::swap(_finish, v._finish);

std::swap(_endofstorage, v._endofstorage);

}

// v1 = v3

vector<T>& operator=(vector<T> v)

{

swap(v);

return *this;

}

~vector()

{

delete[] _start;

_start = _finish = _endofstorage = nullptr;

}

size_t size() const

{

return _finish - _start;

}

T& operator[](size_t pos)

{

assert(pos < size());

return _start[pos];

}

const T& operator[](size_t pos) const

{

assert(pos < size());

return _start[pos];

}

size_t capacity() const

{

return _endofstorage - _start;

}

void reserve(size_t n)

{

if (n > capacity())

{

T* tmp = new T[n];

size_t old_size = size();

//memcpy(tmp, _start, size() * sizeof(T));

for (size_t i = 0; i < old_size; i++)

{

tmp[i] = _start[i];

}

delete[] _start;

_start = tmp;

_finish = tmp + old_size;

_endofstorage = tmp + n;

}

}

void resize(size_t n, const T& val = T())

{

if (n > size())

{

reserve(n);

// 插入

while (_finish < _start + n)

{

*_finish = val;

++_finish;

}

}

else

{

// 删除

_finish = _start + n;

}

}

void push_back(const T& val)

{

insert(end(), val);

}

void pop_back()

{

/*assert(!empty());

--_finish;*/

erase(end()-1);

}

bool empty()

{

return _start == _finish;

}

void insert(iterator pos, const T& val)

{

assert(pos >= _start);

assert(pos <= _finish);

if (_finish == _endofstorage)

{

size_t len = pos - _start;

reserve(capacity() == 0 ? 4 : capacity() * 2);

// 如果扩容了要更新pos

pos = _start + len;

}

iterator it = _finish - 1;

while (it >= pos)

{

*(it + 1) = *it;

--it;

}

*pos = val;

++_finish;

}

iterator erase(iterator pos)

{

assert(pos >= _start);

assert(pos < _finish);

iterator it = pos + 1;

while (it < _finish)

{

*(it - 1) = *it;

++it;

}

--_finish;

return pos;

}

private:

iterator _start = nullptr;

iterator _finish = nullptr;

iterator _endofstorage = nullptr;

};

//template<typename T>

// 函数模板

template<class T>

void print_vector(const vector<T>& v)

{

for (size_t i = 0; i < v.size(); i++)

{

cout << v[i] << " ";

}

cout << endl;

//typename vector<T>::const_iterator it = v.begin();

/*auto it = v.begin();

while (it != v.end())

{

cout << *it << " ";

++it;

}

cout << endl;

for (auto e : v)

{

cout << e << " ";

}

cout << endl;*/

}

void test_vector1()

{

vector<int> v1;

v1.push_back(1);

v1.push_back(2);

v1.push_back(3);

v1.push_back(4);

v1.push_back(4);

v1.push_back(4);

print_vector(v1);

vector<double> v2;

v2.push_back(1.1);

v2.push_back(2.2);

v2.push_back(3.1);

print_vector(v2);

v2.insert(v2.begin(), 11.11);

print_vector(v2);

v2.insert(v2.begin(), 11.11);

print_vector(v2);

v2.insert(v2.begin(), 11.11);

print_vector(v2);

v2.insert(v2.begin(), 11.11);

print_vector(v2);

v2.insert(v2.begin(), 11.11);

print_vector(v2);

v2.erase(v2.begin());

print_vector(v2);

v2.erase(v2.begin()+4);

print_vector(v2);

/*for (size_t i = 0; i < v.size(); i++)

{

cout << v[i] << " ";

}

cout << endl;

vector<int>::iterator it = v.begin();

while (it != v.end())

{

cout << *it << " ";

++it;

}

cout << endl;

for (auto e : v)

{

cout << e << " ";

}

cout << endl;*/

}

void test_vector2()

{

int i = 1;

int j = int();

int k = int(2);

vector<int> v1;

v1.push_back(1);

v1.push_back(2);

v1.push_back(3);

v1.push_back(4);

v1.push_back(4);

v1.push_back(4);

print_vector(v1);

v1.resize(10);

print_vector(v1);

v1.resize(3);

print_vector(v1);

}

void test_vector3()

{

vector<int> v1;

v1.push_back(1);

v1.push_back(2);

v1.push_back(3);

v1.push_back(4);

v1.push_back(4);

v1.push_back(4);

print_vector(v1);

v1.pop_back();

vector<int> v2(v1);

print_vector(v2);

vector<int> v3;

v3.push_back(10);

v3.push_back(20);

v3.push_back(30);

v1 = v3;

print_vector(v1);

print_vector(v3);

}

void test_vector4()

{

vector<int> v1;

v1.push_back(1);

v1.push_back(2);

v1.push_back(3);

v1.push_back(4);

print_vector(v1);

vector<int> v2(v1.begin()+1, v1.end()-1);

print_vector(v2);

string str("abcd");

vector<int> v3(str.begin(), str.end());

print_vector(v3);

}

void test_vector5()

{

vector<int> v1(10, 1);

print_vector(v1);

vector<int> v2(10u, 1);

print_vector(v2);

vector<char> v3(10, 'a');

print_vector(v3);

}

void test_vector6()

{

auto x = { 1,2,3,4,5,6,7,8,9,10 };

cout << typeid(x).name() << endl;

cout << sizeof(x)<< endl;

initializer_list<int> y = { 1,2,3,4,5,6,7 };

// 单参数的构造函数,隐式类型转换

string str = "11111"; // 构造 + 拷贝构造 -> 优化 直接构造

const string& str1 = "11111"; // 构造临时对象,引用的是临时对象

vector<string> v;

v.push_back(str);

v.push_back(string("22222"));

v.push_back("33333");

int i = 1;

// 不推荐 -- C++11

//int j = { 1 };

int k{ 1 };

// 跟上面类似

// 隐式类型转换+优化

//vector<int> v1 = { 1,2,3,4,5,6,7,8,9,10 };

vector<int> v1 { 1,2,3,4,5,6,7,8,9,10 };

for (auto e : v1)

{

cout << e << " ";

}

cout << endl;

// 直接构造

vector<int> v2({ 10,20,30,40 });

for (auto e : v2)

{

cout << e << " ";

}

cout << endl;

}

void test_vector7()

{

// 20:10继续

vector<string> v;

v.push_back("11111");

v.push_back("22222");

v.push_back("33333");

v.push_back("44444");

v.push_back("55555");

for (auto& e : v)

{

cout << e << " ";

}

cout << endl;

}

void test_vector8()

{

vector<int> v1;

v1.push_back(1);

v1.push_back(2);

v1.push_back(3);

v1.push_back(4);

v1.push_back(5);

v1.push_back(6);

v1.push_back(7);

v1.push_back(8);

print_vector(v1);

// insert以后,it就失效了,不要使用了

vector<int>::iterator it = v1.begin()+3;

v1.insert(it, 40);

print_vector(v1);

it = v1.begin() + 3;

cout << *it << endl;

}

void test_vector9()

{

//std::vector<int> v1;

vector<int> v1;

v1.push_back(1);

v1.push_back(2);

v1.push_back(3);

v1.push_back(4);

v1.push_back(4);

v1.push_back(5);

//v1.push_back(4);

// 删除偶数 -- 迭代器失效以后,不要直接使用,如果要使用按规则重新更新后使用

//std::vector<int>::iterator it = v1.begin();

vector<int>::iterator it = v1.begin();

// 21:15

//cout << typeid(it).name() << endl;

while (it != v1.end())

{

if (*it % 2 == 0)

{

it = v1.erase(it);

}

else

{

++it;

}

}

//print_vector(v1);

for (auto e : v1)

{

cout << e << " ";

}

cout << endl;

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值