目录
vector的介绍
vector的使用
construct
iterator
capacity
Element access
mondifiers
vector的原理介绍以及简单实现
vector的介绍
我们平时在写代码中可能会使用各种类型的array,也就是数组,但是这些数组都是静态的,意味着我们只能一开始就确定他们的大小并且存储固定的数据。但是实际应用中需要动态变化的,数组可能时大时小,因此我们需要了解STL库中的vector容器,我们可以将其看作为动态的顺序表,也就是动态的数组,接下来让我们从使用和探寻底层并且实现三个方面来认识vector!
vector的使用
vector的使用包含多个方面,本次着重于常用的函数,其他的功能大家可以自行了解探寻:vector - C++ Reference (cplusplus.com)
construct
void Test_construct()
{
// 无参构造
vector<int> v1;
// 带参构造
vector<int> v2({ 1, 2, 3, 4 });
vector<int> v3(1);
vector<int> v4(5, 1);
// 迭代器构造
vector<int> v5(v4.begin(), v4.end());
// 拷贝构造
vector<int> v6(v5);
// 赋值重载
vector<int> v7;
v7 = v6;
}
iterator
// begin, end
vector<int> v({ 1, 2, 3, 4 });
cout << "正向遍历" << endl;
vector<int>::iterator it = v.begin();
while (it != v.end())
{
cout << *it << " ";
++it;
}
cout << endl;
// rbegin rend
cout << "反向遍历" << endl;
vector<int>::reverse_iterator rit = v.rbegin();
while (rit != v.rend())
{
cout << *rit << " ";
++rit;
}
capacity
vector<int> v{ 1, 2, 3, 4, 5 };
// size 显示v中的数据个数
cout << "size:" << v.size() << endl;
// max_size 表示v中最多可以存储多少个数据
cout << "max_size:" << v.max_size() << endl;
// resize 收缩v中的数据到某个个数值
for (auto e : v)
{
cout << e << " ";
}
cout << endl;
v.resize(2);
for (auto e : v)
{
cout << e << " ";
}
cout << endl;
// resize 如果收缩的值比原本的size大,我们可以初始化他们
v.resize(5);
for (auto e : v)
{
cout << e << " ";
}
cout << endl;
cout << v.capacity() << endl;
vector<int> tmp;
int old_capacity = tmp.capacity();
cout << "tmp grow:" << old_capacity << endl;
for (int i = 0; i < 100; ++i)
{
tmp.push_back(i);
if (tmp.capacity() != old_capacity)
{
old_capacity = tmp.capacity();
cout << old_capacity << endl;
}
}
// empty可以用于判断v中的数据是否为空
if (!v.empty())
{
cout << "非空" << endl;
}
else {
cout << "空" << endl;
}
Element access
int main()
{
// operator[]
// 重载了这个[],我们可以通过下标遍历元素
vector<int> v{ 1,2,3,4,5,6,7,8,9 };
for (int i = 0; i < v.size(); ++i)
{
cout << v[i] << " ";
}
cout << endl;
// front 返回v中第一个元素的引用
// back 返回v中最后一个元素的引用
cout << v.front() << endl;
cout << v.back() << endl;
return 0;
}
mondifiers
int main()
{
vector<int> v;
// push_back 尾插
v.push_back(9);
v.push_back(8);
// pop_back 尾删
v.pop_back();
// insert 插入
v.insert(v.begin(), 10);
// erase 删除
v.erase(v.end() - 1);
// swap 交换
vector<int> v1{ 7,8,9 };
vector<int> v2{ 1,2,3 };
v1.swap(v2);
for (auto e : v1)
{
cout << e << " ";
}
cout << endl;
// clear 清空size,capacity不变
v.clear();
for (auto e : v)
{
cout << e << " ";
}
return 0;
}
vector的原理介绍以及简单实现
#pragma once
#include <iostream>
#include <assert.h>
using namespace std;
namespace ouyang
{
template<class T>
class Vector
{
typedef T* iterator;
typedef const T* const_iterator;
public:
Vector() = default;
Vector(const Vector<T>& v)
{
reserve(v.size());
for (auto& e : v)
{
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 (int 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()
{
if (_start)
{
cout << "~Vector()" << endl;
delete[] _start;
_start = _finish = _end_of_storage = nullptr;
}
}
//写法一:
/*Vector<T>& operator=(const Vector<T>& v)
{
if (this != &v)
{
clear();
reserve(v.size());
for (auto& e : v)
{
push_back(e);
}
}
return *this;
}*/
// 写法二:
Vector<T>& operator=(Vector<T> v)
{
Swap(v);
return *this;
}
void clear()
{
_finish = _start;
}
void Swap(Vector<T>& v)
{
swap(_start, v._start);
swap(_finish, v._finish);
swap(_end_of_storage, v._end_of_storage);
}
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 _end_of_storage - _start;
}
bool empty()
{
return _start == _finish;
}
void reserve(size_t n)
{
if (n > capacity())
{
size_t oldsize = size();
T* tmp = new T[n];
memcpy(tmp, _start, sizeof(T) * size());
delete[] _start;
_start = tmp;
_finish = tmp + oldsize;
_end_of_storage = tmp + n;
}
}
iterator insert(iterator pos, const T& val)
{
assert(pos >= _start);
assert(pos <= _finish);
if (_finish == _end_of_storage)
{
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 = val;
++_finish;
return pos;
}
void erase(iterator pos)
{
assert(pos >= _start);
assert(pos < _finish);
iterator it = pos + 1;
while (it != end())
{
*(it - 1) = *it;
++it;
}
--_finish;
}
void push_back(const T& val)
{
if (_finish == _end_of_storage)
{
reserve(capacity() == 0 ? 4 : capacity() * 2);
}
*_finish = val;
++_finish;
}
void pop_back()
{
assert(!empty());
--_finish;
}
T& operator[](size_t i)
{
assert(i < size());
return _start[i];
}
const T& operator[](size_t i) const
{
assert(i < size());
return _start[i];
}
private:
iterator _start = nullptr;
iterator _finish = nullptr;
iterator _end_of_storage = nullptr;
};
template<class Container>
void Print(const Container& object)
{
/*auto it = object.begin();
while (it != object.end())
{
cout << *it << " ";
++it;
}
cout << endl;*/
for (auto e : object)
{
cout << e << " ";
}
cout << endl;
}
}
#include "vector.h"
int main()
{
ouyang::Vector<int> v1;
v1.push_back(1);
v1.push_back(2);
v1.push_back(3);
v1.push_back(4);
v1.push_back(5);
/*auto it = v1.begin();
while (it != v1.end())
{
cout << *it << " ";
++it;
}
cout << endl;
ouyang::Vector<int> v2(v1);
for (int i = 0; i < v2.size(); ++i)
{
cout << v2[i] << " ";
}
cout << endl;*/
/*ouyang::Vector<int> v3(v1.begin(), v1.end());
auto it = v3.begin();
while (it != v3.end())
{
cout << *it << " ";
++it;
}
cout << endl;*/
//ouyang::Vector<int> v4(5);
//ouyang::Vector<int> v5(5, 1);
//ouyang::Print(v4);
//ouyang::Print(v5);
//v5.clear();
//ouyang::Print(v4);
//ouyang::Print(v5);
/*ouyang::Vector<int> v6;
v6 = v1;
ouyang::Print(v6);
v6.insert(v6.begin(), 100);
ouyang::Print(v6);
v6.erase(v6.begin());
ouyang::Print(v6);*/
return 0;
}