一. vector的介绍
1.vector的介绍
二.vector的使用
三.vector的模拟实现
#include
#include
using namespace std;
void Test_vector2()
{
size_t sz;
vector<int> v;
sz = v.capacity();
cout << "making v grow:\n";
for (int i = 0; i < 100; ++i)
{
v.push_back(i);
if (sz != v.capacity())
{
sz = v.capacity();
cout << "capacity changed: " << sz << '\n';
}
}
}
int main()
{
Test_vector2();
return 0;
}
vs:运行结果:vs下使用的STL基本是按照1.5倍方式扩容
通过reserve()接口提前设置好容量,我们可以减少扩容次数,减少扩容对效率的损耗
测试代码:
#include
#include
using namespace std;
void Test_vector1()
{
vector<int> v;
for (size_t i = 0; i < 10; i++)
{
v.push_back(i);
}
}
void Test_vector2()
{
size_t sz;
vector<int> v;
sz = v.capacity();
cout << "making v grow:\n";
for (int i = 0; i < 100; ++i)
{
v.push_back(i);
if (sz != v.capacity())
{
sz = v.capacity();
cout << "capacity changed: " << sz << '\n';
}
}
}
void Test_vector3()
{
vector<int> v;
size_t sz = v.capacity();
v.reserve(100); // 提前将容量设置好,可以避免一遍插入一遍扩容
cout << "making bar grow:\n";
for (int i = 0; i < 100; ++i)
{
v.push_back(i);
if (sz != v.capacity())
{
sz = v.capacity();
cout << "capacity changed: " << sz << '\n';
}
}
}
int main()
{
Test_vector3();
return 0;
}
void Test_vector1()
{
vector<int> v;
for (size_t i = 0; i < 10; i++)
{
v.push_back(i);
}
cout << "尾插数据后:" << endl;
for (size_t i = 0; i < v.size(); i++)
{
cout << v[i] << " ";//像数组一样用[]访问vector中的元素
}
cout << endl;
v.pop_back();
v.pop_back();
cout << "尾删两个数据后:" << endl;
for (size_t i = 0; i < v.size(); i++)
{
cout << v[i] << " ";
}
cout << endl;
vector<int>::iterator it = v.begin();
v.insert(it+3, 30);//在下标为3的前面插入一个30
for (size_t i = 0; i < v.size(); i++)
{
cout << v[i] << " ";
}
cout << endl;
it = v.erase(it+3);//删除it位置的数,并把该位置被删除的后面的数的迭代器返回给it
for (size_t i = 0; i < v.size(); i++)
{
cout << v[i] << " ";
}
cout << endl;
}
测试代码:
void Test_vector4()
{
vector<int> v;
for (size_t i = 0; i < 10; i++)
{
v.push_back(i);
}
cout << "第一种用[]进行遍历:" << endl;
for (size_t i = 0; i < v.size(); i++)
{
cout << v[i] << " ";
}
cout << endl;
cout << "第二种用迭代器进行遍历:" << endl;
vector<int>::iterator it = v.begin();
while (it != v.end())
{
cout << *it << " ";
it++;
}
cout << endl;
cout << "第三种用范围for进行遍历:" << endl;
for (auto e : v)
{
cout << e << " ";
}
cout << endl;
}
开始时定义个自己的命名空间,然后利用类模板控制vector可以存放多种类型的数据,定义迭代器和私有成员变量,_start相当于vector常用接口中的begin(),_finish相当于end(),_endofstorage相当于_start+capacity();
#pragma once
#include
#include
#include
using namespace std;
namespace simulation
{
template<class T>
class vector
{
public:
typedef T* iterator;
typedef const T* const_iterator;
private:
iterator _start;
iterator _finish;
iterator _endofstorage;
};
}
代码:
//尾插
void push_back(const T& x)
{
if (_finish == _endofstorage)
{
reserve(capacity() == 0 ? 4 : capacity() * 2);
}
*_finish = x;
_finish++;
}
//在pos位置前插入x
void insert(iterator pos, const T& x)
{
assert(pos >= _start);
assert(pos <= _finish);
if (_finish == _endofstorage)
{
size_t len = pos - _start;
reserve(capacity() == 0 ? 4 : capacity() * 2);
pos = _start + len;
}
iterator end = _finish - 1;
while (end >= pos)
{
*(end + 1) = *end;
end--;
}
*pos = x;
_finish++;
}
//删除pos位置的元素
iterator erase(iterator pos)
{
assert(pos >= _start);
assert(pos <= _finish);
iterator it = pos+1;
while (it<_finish)
{
*(it-1) = *it;
++it;
}
_finish--;
return pos;
}
测试代码:
void test_vector2()
{
simulation::vector<int> v;
//尾插
v.push_back(1);
v.push_back(2);
v.push_back(3);
v.push_back(4);
v.push_back(5);
v.push_back(6);
v.push_back(7);
v.push_back(8);
for (int e : v)
{
cout << e << " ";
}
cout << endl;
//在4前面插入一个40
v.insert(v.begin() + 3, 40);
for (int e : v)
{
cout << e << " ";
}
cout << endl;
//头插一个100
v.insert(v.begin(), 100);
for (int e : v)
{
cout << e << " ";
}
cout << endl;
//尾插1000
v.insert(v.end(), 1000);
for (int e : v)
{
cout << e << " ";
}
cout << endl;
//头删
v.erase(v.begin());
for (int e : v)
{
cout << e << " ";
}
cout << endl;
//尾删
v.erase(v.end() - 1);
for (int e : v)
{
cout << e << " ";
}
cout << endl;
}
成员函数实现:
void reserve(size_t n)
{
if (n > capacity())
{
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;
}
}
void resize(size_t n, const T& val = T())
{
if (n <= _finish)
{
_finish = _start + n;
}
else
{
reserve(n);
while (n + _start > _finish)
{
*_finish = val;
_finish++;
}
}
}
erase这个接口也会使得迭代器失效,原因在于删除pos位置的数据之后,pos指向的数据的值已经被删除,所以想要继续使用迭代器需要在该接口返回值那里返回一个迭代器,指向被删除的值的下一个位置的迭代器,方能继续使用.
所以这样实现才是合理的
//删除pos位置的元素
iterator erase(iterator pos)
{
assert(pos >= _start);
assert(pos <= _finish);
iterator it = pos+1;
while (it<_finish)
{
*(it-1) = *it;
++it;
}
_finish--;
return pos;
}
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);
}
}
vector()
:_start(nullptr)
,_finish(nullptr)
,_endofstorage(nullptr)
{
}
vector(const vector<T>& v)
:_start(nullptr)
, _finish(nullptr)
, _endofstorage(nullptr)
{
reserve(v.capacity());
for (auto& e : v)
{
push_back(e);
}
}
vector<T>& operator=(vector<T> tmp)
{
swap(tmp);
return *this;
}
void swap(vector<T>& v)
{
std::swap(_start, v._start);
std::swap(_finish, v._finish);
std::swap(_endofstorage, v._endofstorage);
}
~vector()
{
delete[] _start;
_start = _finish = _endofstorage = nullptr;
}
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];
}
T& operator[](size_t pos) const
{
assert(pos < size());
return _start[pos];
}
vector(const vector<T>& v)
:_start(nullptr)
, _finish(nullptr)
, _endofstorage(nullptr)
{
reserve(v.capacity());
for (auto& e : v)
{
push_back(e);
}
}
vector<T>& operator=(vector<T> tmp)
{
swap(tmp);
return *this;
}
void swap(vector<T>& v)
{
std::swap(_start, v._start);
std::swap(_finish, v._finish);
std::swap(_endofstorage, v._endofstorage);
}
void reserve(size_t n)
{
if (n > capacity())
{
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;
}
}
通过for循环一份一份的拷贝到新的空间就不会造成空间被释放两次
#pragma once
#include
#include
#include
using namespace std;
namespace simulation
{
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;
}
size_t size() const
{
return _finish - _start;
}
size_t capacity() const
{
return _endofstorage - _start;
}
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);
}
}
vector()
:_start(nullptr)
, _finish(nullptr)
, _endofstorage(nullptr)
{
}
vector(const vector<T>& v)
:_start(nullptr)
, _finish(nullptr)
, _endofstorage(nullptr)
{
reserve(v.capacity());
for (auto& e : v)
{
push_back(e);
}
}
vector<T>& operator=(vector<T> tmp)
{
swap(tmp);
return *this;
}
void swap(vector<T>& v)
{
std::swap(_start, v._start);
std::swap(_finish, v._finish);
std::swap(_endofstorage, v._endofstorage);
}
~vector()
{
delete[] _start;
_start = _finish = _endofstorage = nullptr;
}
T& operator[](size_t pos)
{
assert(pos < size());
return _start[pos];
}
T& operator[](size_t pos) const
{
assert(pos < size());
return _start[pos];
}
void reserve(size_t n)
{
if (n > capacity())
{
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;
}
}
void resize(size_t n, const T& val = T())
{
if (n <= _finish)
{
_finish = _start + n;
}
else
{
reserve(n);
while (n + _start > _finish)
{
*_finish = val;
_finish++;
}
}
}
void push_back(const T& x)
{
if (_finish == _endofstorage)
{
reserve(capacity() == 0 ? 4 : capacity() * 2);
}
*_finish = x;
_finish++;
}
void insert(iterator pos, const T& x)
{
assert(pos >= _start);
assert(pos <= _finish);
if (_finish == _endofstorage)
{
size_t len = pos - _start;
reserve(capacity() == 0 ? 4 : capacity() * 2);
pos = _start + len;
}
iterator end = _finish - 1;
while (end >= pos)
{
*(end + 1) = *end;
end--;
}
*pos = x;
_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;
iterator _finish;
iterator _endofstorage;
};
}