【C++】vector 常用成员函数的模拟实现

六点半888 2024-09-30 14:05:03 阅读 53

【C++】vector 常用成员函数的模拟实现

1. vector 常用成员函数的模拟实现2. vector 常用成员函数实现后的测试

1. vector 常用成员函数的模拟实现

2. vector 常用成员函数实现后的测试

<code>#include<assert.h>

#include<iostream>

#include<string>

using std::cout;

using std::endl;

using std::string;

//自定义命名空间wch

namespace wch

{ -- -->

//vector要支持不同类型元素,所以用模板

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<int> v(10, 1);//如果无函数vector(int n, const T& val = T()),

//因为参数类型更匹配,会去调用vector(InputIterator first, InputIterator last)

// 但是函数内部有解引用(*first),整形不可解引用,导致报错

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

{

resize(n, val);

}

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

{

resize(n, val);

}

// [first, last)迭代器,采用区间初始化

template<class InputIterator>

vector(InputIterator first, InputIterator last)

{

while (first != last)

{

push_back(*first);

++first;

}

}

//由于声明时已经初始化给予了缺省参数,所以此处不写初始化列表初始化

vector()

{ }

//原始方法实现拷贝构造函数

vector(const vector<T>& v)

{

_start = new T[v.capacity()];

//memcpy(_start, v._start, sizeof(T)*v.size());//浅拷贝,部分自定义类型无法达到目的

//深拷贝

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

{

_start[i] = v._start[i];

}

_finish = _start + v.size();

_endofstorage = _start + v.capacity();

}

//现代方法拷贝构造

/*vector(const vector<T>& v)

:_start(nullptr)

, _finish(nullptr)

, _endofstorage(nullptr)

{

reserve(v.capacity());

for (auto e : v)

{

push_back(e);

}

}*/

void swap(vector<T>& v)

{

//注意这里用的是库函数swap,因为我们这里是在自己实现vector成员函数

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

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

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

}

// v1 = v2,赋值运算符重载

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

{

swap(v);

return *this;

}

~vector()

{

if (_start)

{

delete[] _start;

_start = _finish = _endofstorage = nullptr;

}

}

void reserve(size_t n)

{

//要加判断,因为当n<capacity时,不需要做处理

if (n > capacity())

{

//这里需要先保存大小,因为开辟新空间后,成员函数size里的地址_finish还指向销毁的旧空间,

//无法正确完成_finish的更新

size_t sz = size();

T* tmp = new T[n];

if (_start)

{

//memcpy(tmp, _start, sizeof(T) * sz);//浅拷贝

//深拷贝

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

{

tmp[i] = _start[i];

}

delete[] _start;

}

_start = tmp;

_finish = _start + sz;

_endofstorage = _start + n;

}

}

//缺省参数T(),当缺省参数为自定义类型,T()为匿名对象,会去调用构造函数

//内置类型如:int与int()无差别

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

{

if (n < size())

{

_finish = _start + n;

}

else

{

reserve(n);

//多余有效空间用val初始化

while (_finish != _start + n)

{

*_finish = val;

++_finish;

}

}

}

void push_back(const T& x)

{

/*if (_finish == _endofstorage)

{

size_t newcapacity = capacity() == 0 ? 4 : capacity() * 2;

reserve(newcapacity);

}

*_finish = x;

++_finish;*/

insert(end(), x);

}

void pop_back()

{

erase(--end());

}

size_t capacity() const

{

return _endofstorage - _start;

}

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];

}

//返回插入元素地址

iterator insert(iterator pos, const T& x)

{

assert(pos >= _start && pos <= _finish);

if (_finish == _endofstorage)

{

size_t len = pos - _start;// 解决pos迭代器失效问题

size_t newcapacity = capacity() == 0 ? 4 : capacity() * 2;

reserve(newcapacity);

// 需要更新pos,因为扩容过后pos还指向旧空间,即解决pos迭代器失效问题

pos = _start + len;

}

iterator end = _finish - 1;

while (end >= pos)

{

*(end + 1) = *end;

--end;

}

*pos = x;

++_finish;

return pos;

}

//返回删除元素地址

iterator erase(iterator pos)

{

assert(pos >= _start && 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;

};

void print(const vector<int>& v)

{

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(5);

for (auto e : v1)

{

cout << e << " ";

}

cout << endl;

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

{

v1[i]++;

}

for (auto e : v1)

{

cout << e << " ";

}

cout << endl;

print(v1);

}

void test_vector2()

{

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(5);

v1.push_back(5);

v1.push_back(5);

for (auto e : v1)

{

cout << e << " ";

}

cout << endl;

v1.insert(v1.begin(), 100);

for (auto e : v1)

{

cout << e << " ";

}

cout << endl;

/*vector<int>::iterator p = v1.begin() + 3;

v1.insert(p, 300);*/

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

//v1.insert(p+3, 300);

// insert以后迭代器可能会失效(扩容)

// 记住,insert以后就不要使用这个形参迭代器了,因为他可能失效了

v1.insert(p, 300);

// 高危行为

// *p += 10;

for (auto e : v1)

{

cout << e << " ";

}

cout << endl;

}

void test_vector3()

{

vector<int> v1;

v1.push_back(1);

v1.push_back(2);

v1.push_back(2);

v1.push_back(3);

v1.push_back(4);

v1.push_back(5);

v1.push_back(6);

for (auto e : v1)

{

cout << e << " ";

}

cout << endl;

auto it = v1.begin();

while (it != v1.end())

{

if (*it % 2 == 0)

{

it = v1.erase(it);

}

else

{

++it;

}

}

//v1.erase(v1.begin());

//auto it = v1.begin()+4;

//v1.erase(it);

erase以后,迭代器失效了,不能访问

vs进行强制检查,访问会直接报错

//cout << *it << endl;

//++it;

//cout << *it << endl;

for (auto e : v1)

{

cout << e << " ";

}

cout << endl;

}

void test_vector4()

{

vector<int> v;

v.resize(10, 0);

for (auto e : v)

{

cout << e << " ";

}

cout << endl;

int i = 0;

int j = int();//i = 0;

int k = int(1);//k = 1;

}

void test_vector5()

{

vector<int> v;

v.push_back(1);

v.push_back(2);

v.push_back(3);

v.push_back(4);

v.push_back(5);

vector<int> v1(v);

for (auto e : v1)

{

cout << e << " ";

}

cout << endl;

vector<int> v2;

v2.resize(10, 1);

v1 = v2;

for (auto e : v1)

{

cout << e << " ";

}

cout << endl;

}

void test_vector6()

{

vector<string> v;

v.push_back("111111111111111111");

v.push_back("222222222222222222");

v.push_back("333333333333333333");

v.push_back("444444444444444444");

v.push_back("555555555555555555");

for (auto& e : v)

{

cout << e << " ";

}

cout << endl;

vector<string> v1(v);

for (auto& e : v1)

{

cout << e << " ";

}

cout << endl;

}

void test_vector7()

{

vector<int> v(10, 1);

vector<string> v1(10, "1111");

vector<int> v2(10, 1);

for (auto e : v)

{

cout << e << " ";

}

cout << endl;

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

for (auto e : v3)

{

cout << e << " ";

}

cout << endl;

string str("hello world");

vector<char> v4(str.begin(), str.end());

for (auto e : v4)

{

cout << e << " ";

}

cout << endl;

int a[] = { 16,2,77,29 };

vector<int> v5(a, a + 4);

for (auto e : v5)

{

cout << e << " ";

}

cout << endl;

}

}

int main()

{

//wch::test_vector1();

//wch::test_vector2();

//wch::test_vector3();

//wch::test_vector4();

//wch::test_vector5();

//wch::test_vector6();

wch::test_vector7();

return 0;

}



声明

本文内容仅代表作者观点,或转载于其他网站,本站不以此文作为商业用途
如有涉及侵权,请联系本站进行删除
转载本站原创文章,请注明来源及作者。