文章目录
Ⅰ.vector的用法
1.vector对象的创建(构造)
void test_vector1()
{
// 为开辟空间大小
vector<int> v0;
//10 个1
vector<int> v1(10, 1);
// 10个1
vector<int> v2(v1.begin(),v1.end());
// 拷贝构造
vector<int> v3(v2);
for (auto e : v0) cout << e; cout << endl;
for (auto e : v1) cout << e; cout << endl;
for (auto e : v2) cout << e; cout << endl;
for (auto e : v3) cout << e; cout << endl;
}
2.vector销毁(析构)
声明周期结束时,编译器自动调用
3.vecotr遍历
第一种下标方括号:
void test_vector2()
{
vector<int> v(4, 7);
for (int i = 0; i < v.size(); i++)
cout << v[i];
cout << endl;
}
第二种迭代器:
void test_vector2()
{
vector<int> v(4, 7);
vector<int>::iterator it = v.begin();
while (it != v.end())
{
cout << *it << ' ';
it++;
}
}
第三种范围for:
void test_vector2()
{
vector<int> v(4, 7);
for (int e : v)
cout << e;
}
4.vector增
函数名称 | 功能介绍 |
---|---|
push_back(x) | 向vector的尾部插入一个数据 |
insert(it,x) | 向该迭代器的位置插入一个数据 |
void test_vector3()
{
// 尾插
vector<int> v;
v.push_back(1);
v.push_back(2);
v.push_back(3);
v.push_back(4);
//1 2 3 4
}
void test_vector3()
{
vector<int> v(4, 8);
vector<int>::iterator it = v.begin();
v.insert(it, 10);
// 10 8 8 8 8
}
5.vector删
函数名称 | 功能介绍 |
---|---|
pop_back(x) | 在vector的尾删一个数据 |
erase(it) | 向该迭代器的位置删除一个数据,并返回指向该位置的新的迭代器 |
clear() | 清空数据 |
void test_vector4()
{
vector<int> v;
v.push_back(1);
v.push_back(2);
v.push_back(3);
v.push_back(4);
v.pop_back();
//1 2 3
}
void test_vector4()
{
vector<int> v;
v.push_back(1);
v.push_back(2);
v.push_back(3);
v.push_back(4);
vector<int>::iterator it = v.begin();
v.erase(it + 1);
// 1 3 4
}
void test_vector4()
{
vector<int> v(4, 8);
v.clear();
}
6.vector查
vector的库里边并没有存在类似find()的接口,说明vector一般不使用查找的功能
7.vector改
函数名称 | 功能介绍 |
---|---|
assign(n,x) | 使用n个值为x去覆盖原vector的内容 |
void test_vector4()
{
vector<int> v;
v.push_back(1);
v.push_back(2);
v.push_back(3);
v.push_back(4);
v.assign(4, 1);
Ⅱ.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()
{}
vector(int n ,const T& x = T())
{
/*_start = new T[n];
for (int i = 0; i < n; i++)
{
_start[i] = x;
}
_finish = _start + n;
_endofstorage = _start + n;*/
reserve(n);
for (int i = 0; i < n; i++)
{
push_back(x);
}
}
vector(size_t n, const T& x = T())
{
reserve(n);
for (int i = 0; i < n; i++)
{
push_back(x);
}
}
template <class InputIterator>
vector(InputIterator first, InputIterator last)
{
reserve(first - last);
while (first != last)
{
push_back(*first);
++first;
}
}
vector(const vector<T>& v)
{
reserve(v.capacity());
for (auto& e : v)
{
push_back(e);
}
}
vector<T>& operator=(vector<T> tmp)
{
swap(tmp);
return *this;
}
~vector()
{
delete[] _start;
_start = _finish = _endofstorage = nullptr;
}
T& operator[](size_t pos)
{
assert(pos >= 0 && pos < size());
return _start[pos];
}
const T& operator[](size_t pos) const
{
assert(pos >= 0 && pos < size());
return _start[pos];
}
void reserve(size_t n)
{
if (n > capacity())
{
T* tmp = new T[n];
size_t len = size();
for (size_t i = 0; i < len; i++)
{
tmp[i] = _start[i];
}
delete _start;
_start = tmp;
_finish = _start + len;
_endofstorage = _start + n;
}
}
void resize(size_t n,const T& x = T())
{
if (n <= size())
{
_finish = _start + n;
}
else
{
reserve(n);
while (_finish < _start + n)
{
*_finish = x;
_finish++;
}
}
}
bool empty()
{
return _start == _finish;
}
void push_back(const T& x)
{
/*if (size() == capacity())
{
reserve(capacity() == 0 ? 4 : 2 * capacity());
}
*_finish = x;
_finish++;*/
insert(end(), x);
}
void pop_back()
{
erase(end()-1);
}
void swap(vector<T>& v)
{
std::swap(_start, v._start);
std::swap(_finish, v._finish);
std::swap(_endofstorage, v._endofstorage);
}
size_t size() const
{
return _finish - _start;
}
size_t capacity() const
{
return _endofstorage - _start;
}
void insert(iterator pos, const T& x)
{
assert(pos >= _start && pos <= _finish);
if (size() == capacity())
{
size_t len = pos - _start;
reserve(capacity() == 0 ? 4 : 2 * capacity());
pos = _start + len;
}
iterator it = _finish - 1;
while (it >= pos)
{
*(it + 1) = *(it);
it--;
}
*pos = x;
_finish++;
}
iterator erase(iterator pos)
{
assert(pos >= _start && pos < _finish);
iterator it = pos;
while (it != _finish)
{
*it = *(it + 1);
it++;
}
_finish--;
return pos;
}
private:
iterator _start = nullptr;
iterator _finish = nullptr;
iterator _endofstorage = nullptr;
};