与string有很强的相似性
不支持流提取,流插入的直接打印
需要包含的头文件
#include
#include
#include
#include
#include
using namespace std;
主函数:
int main()
{
std::test_vector5();
Solution().generate(5);
return 0;
}
构造函数:类模板
namespace std
{
void test_vector1()
{
vector<int> v1;
vector<int> v2(10, 1);
vector<int> v3(v2);
}
遍历:
下标+[ ] 像数组一样访问,可读可写(得益于运算符重载)
迭代器(4种) 像指针一样 支持范围for(底层就是迭代器)
void test_vector2()
{
vector<int> v1;
v1.push_back(1);
v1.push_back(2);
v1.push_back(3);
v1.push_back(4);
// 下标+[]
for (size_t i = 0; i < v1.size(); ++i)
{
v1[i]++;
}
for (size_t i = 0; i < v1.size(); ++i)
{
cout << v1[i] << " ";
}
cout << endl;
// 迭代器
vector<int>::iterator it = v1.begin();
while (it != v1.end())
{
(*it)--;
cout << *it << " ";
++it;
}
cout << endl;
for (auto e : v1)
{
cout << e << " ";
}
cout << endl;
}
只提供尾插尾删
void TestVectorExpand()
{
size_t sz;
vector<int> v;
//避免扩容
//v.resize(100);
v.reserve(100);//不会影响数据
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> v1;
v1.push_back(1);
v1.push_back(2);
v1.push_back(3);
v1.push_back(4);
cout << v1.max_size() << endl;
TestVectorExpand();
}
其实只是string比较特殊,其余的都是有通用的find函数模板
左闭右开[first,last)
void test_vector4()
{
vector<int> v1;
v1.push_back(1);
v1.push_back(2);
v1.push_back(3);
v1.push_back(4);
vector<int>::iterator pos = find(v1.begin(), v1.end(), 3);
if (pos != v1.end())
{
v1.insert(pos, 30);
}
for (auto e : v1)
{
cout << e << " ";
}
cout << endl;
pos = find(v1.begin(), v1.end(), 300);
if (pos != v1.end())
{
v1.erase(pos);
}
for (auto e : v1)
{
cout << e << " ";
}
cout << endl;
}
泛型编程
底层是快排 传迭代区间 函数模板 默认是升序
降序:仿函数
string也可排
void test_vector5()
{
vector<int> v1;
v1.push_back(10);
v1.push_back(2);
v1.push_back(32);
v1.push_back(4);
v1.push_back(5);
v1.push_back(1);
v1.push_back(9);
for (auto e : v1)
{
cout << e << " ";
}
cout << endl;
sort(v1.begin(), v1.end());
for (auto e : v1)
{
cout << e << " ";
}
cout << endl;
//降序
//less ls;
//greater gt;
//sort(v1.begin(), v1.end(), gt);
sort(v1.begin(), v1.end(), greater<int>());
for (auto e : v1)
{
cout << e << " ";
}
cout << endl;
string s("hello313131244");
//sort(s.begin(), s.end());
sort(s.begin(), s.end(), greater<char>());
cout << s << endl;
//vector v;
//string str; // 数据结尾\0 +=、find、比较大小、to_string、<< 、>>等等
// vector 无法替代string
vector<string> strV;
string str1("张三");
strV.push_back(str1);
strV.push_back(string("李四"));
strV.push_back("王六");
strV.push_back("赵七");
//小心深拷贝——加引用的意义
//不加就是拷贝构造——深拷贝
for (const auto& str : strV)
{
cout << str << endl;
}
}
vector.h
#pragma once
namespace haha
{
tmeplate<class T>
class vector
{
typedef T* iterator;
public:
private:
iterator _start;
iterator _finish;
iterator _end_of_storage;
};
}
从源代码中看vector的主要框架:
成员变量的定义:
iterator start;//begin
iterator finish;//end 最后一个数据的下一个位置
iterator end_of_storage;
三个迭代器其实也就是原生指针
原生指针要在连续的物理空间中使用,list就不能用(有结点)
初始化全为空:
vector(): start(0),finish(0),end_of_storage(0)
{}
现在:
size->end()-begin()
capacity->end_of_storage-begin()
从前:
_a;
_size;
_capacity;
//构造函数
vector()
:_start(nullptr)
, _finish(nullptr)
, _end_of_storage(nullptr)
{}
//析构函数
~vector()
{
delete[] _start;
_start = _finish = _end_of_storage = nullptr;
}
size_t size() const
{
return _finish - _start;
}
size_t capacity() const
{
return _end_of_storage - _start;
}
//帮助我们提前开空间
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);
delete[] _start;
}
//重置
//需要提前保留size,避免finish出现问题
_start = tmp;
_finish = _start + sz;
_end_of_storage = _start + n;
}
}
void push_back(const T& x)//T代表自定义类型
{
if (_finish == _end_of_storage)
{
//扩容
reserve(capacity() == 0 ? 4 : capacity() * 2);
}
//放数据
*_finish = x;
++_finish;
}
void pop_back()
{
assert(_finish > _start);
--_finish;
}
这里push_back要用const+&
要用引用而不是传值(深拷贝)
在隐式类型转换中会有临时变量——>具有常性
const T& operator[](size_t pos) const
{
assert(pos < size());
return _start[pos];
}
T& operator[](size_t pos)
{
assert(pos < size());
return _start[pos];
}
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;
}
insert:
将pos位置后的数据挪动一位,再插入数据
erase:
从前往后去覆盖
自己实现的erase不会发生迭代器失效,但是库的有可能会
缩容(以时间换空间)情况下会发生
void push_back(const T& x)
{
/*if (_finish == _end_of_storage)
{
reserve(capacity() == 0 ? 4 : capacity() * 2);
}
*_finish = x;
++_finish;*/
insert(end(), x);
}
void insert(iterator pos, const T& x)
{
assert(pos >= _start);
assert(pos <= _finish);//这里可以用<=是因为push_back可以复用
if (_finish == _end_of_storage)
{
//内部迭代器失效:
//扩容时,释放旧空间,pos指针会失效出现野指针问题
//需要更新pos指针
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;
}
// stl 规定erase返回删除位置下一个位置迭代器
iterator erase(iterator pos)
{
assert(pos >= _start);
assert(pos < _finish);
iterator begin = pos + 1;
while (begin < _finish)
{
*(begin - 1) = *begin;
++begin;
}
--_finish;
//if (size() < capacity()/2)
//{
缩容 -- 以时间换空间
//}
return pos;
}
void test_vector2()
{
vector<int> v;
v.push_back(1);
v.push_back(2);
v.push_back(3);
v.push_back(4);
//v.push_back(5);
for (auto e : v)
{
cout << e << " ";
}
cout << endl;
auto p = find(v.begin(), v.end(), 3);
if (p != v.end())
{
//外部迭代器失效:
//pos的修改不会影响p,形参的修改不会影响实参
// 在p位置插入数据以后,不要访问p,因为p可能失效了。
v.insert(p, 30);
//cout << *p << endl;
//v.insert(p, 40);
}
for (auto e : v)
{
cout << e << " ";
}
cout << endl;
v.insert(v.begin(), 1);
}
void test_vector3()
{
vector<int> v;
v.push_back(1);
v.push_back(2);
v.push_back(3);
v.push_back(4);
for (auto e : v)
{
cout << e << " ";
}
cout << endl;
auto p = find(v.begin(), v.end(), 3);
if (p != v.end())
{
v.erase(p);
}
for (auto e : v)
{
cout << e << " ";
}
cout << endl;
v.erase(v.begin());
for (auto e : v)
{
cout << e << " ";
}
cout << endl;
}
结论:
insert,erase 之后不要直接访问pos。再次访问可能会出现问题
迭代器失效
:
解决
:
//3种错误结果
//void test_vector4()
//{
正常运行
//vector v;
//v.push_back(1);
//v.push_back(2);
//v.push_back(3);
//v.push_back(4);
//v.push_back(5);
要求删除所有的偶数
//auto it = v.begin();
//while (it != v.end())
//{
//if (*it % 2 == 0)
//{
//v.erase(it);
//}
//++it;
//}
//for (auto e : v)
//{
//cout << e << " ";
//}
//cout << endl;
//}
//void test_vector4()
//{
崩溃
//vector v;
//v.push_back(1);
//v.push_back(2);
//v.push_back(3);
//v.push_back(4);
要求删除所有的偶数
//auto it = v.begin();
//while (it != v.end())
//{
//if (*it % 2 == 0)
//{
//v.erase(it);
//}
//++it;
//}
//for (auto e : v)
//{
//cout << e << " ";
//}
//cout << endl;
//}
//void test_vector4()
//{
结果不对
//vector v;
//v.push_back(1);
//v.push_back(2);
//v.push_back(4);
//v.push_back(3);
//v.push_back(4);
//v.push_back(5);
要求删除所有的偶数
//auto it = v.begin();
//while (it != v.end())
//{
//if (*it % 2 == 0)
//{
//v.erase(it);
//}
//
//++it;
//}
//for (auto e : v)
//{
//cout << e << " ";
//}
//cout << endl;
//}
// 正确的方式
void test_vector4()
{
vector<int> v;
v.push_back(1);
v.push_back(2);
v.push_back(4);
v.push_back(3);
v.push_back(4);
v.push_back(5);
// 要求删除所有的偶数
auto it = v.begin();
while (it != v.end())
{
if (*it % 2 == 0)
{
it = v.erase(it);
}
else
{
++it;
}
}
for (auto e : v)
{
cout << e << " ";
}
cout << endl;
}
}
数据排列的偶然性:
- 最后一个不是偶数
- 没有连续的偶数
接收返回值再连跳两次(insert)
auto it = v.begin();
while (it != v.end())
{
if (*it % 2 == 0)
{
it = v.insert(it,*it * 2);
++it;
++it;
}
else
{
++it;
}
}
内置类型
:默认是浅拷贝->指向同一块空间
问题:析构2次+一个对象的修改会影响另一个对象
自定义类型
使用深拷贝:
v1(v2)
//1.
// v2(v1)
//vector(const vector& v)
//{
//_start = new T[v.size()]; // v.capacity()也可以
//memcpy(_start, v._start, sizeof(T)*v.size());
//_finish = _start + v.size();
//_end_of_storage = _start + v.size();
//}
//2.
// v2(v1)
/*vector(const vector& v)
:_start(nullptr)
, _finish(nullptr)
, _end_of_storage(nullptr)
{
reserve(v.size());
for (const auto& e : v)
{
push_back(e);
}
}*/
vector(size_t n, const T& val = T())
:_start(nullptr)
, _finish(nullptr)
, _end_of_storage(nullptr)
{
reserve(n);
for (size_t i = 0; i < n; ++i)
{
push_back(val);
}
}
//3.
template <class InputIterator>
vector(InputIterator first, InputIterator last)
:_start(nullptr)
, _finish(nullptr)
, _end_of_storage(nullptr)
{
while(first != last)
{
push_back(*first);
++first;
}
}
void swap(vector<T>& v)
{
std::swap(_start, v._start);
std::swap(_finish, v._finish);
std::swap(_end_of_storage, v._end_of_storage);
}
// v2(v1)
vector(const vector<T>& v)
:_start(nullptr)
, _finish(nullptr)
, _end_of_storage(nullptr)
{
vector<T> tmp(v.begin(), v.end());
swap(tmp);
}
针对的是两个已经存在的对象
vs = v;
vector<int> copy = v;//copy(v) 拷贝构造
v1 = v2
// v1 = v2
vector<T>& operator=(vector<T> v)
{
swap(v);
return *this;
}
注意:reserve不能用[]访问
void resize(size_t n, const T& val = T())
{
if (n > capacity())
{
reserve(n);
}
if (n > size())
{
// 初始化填值
while (_finish < _start + n)
{
*_finish = val;
++_finish;
}
}
else
{
_finish = _start + n;
}
}
void test_vector10()
{
vector<int> v1;
v1.resize(10, 0);
for (auto e : v1)
{
cout << e << " ";
}
cout << endl;
//std::vector v2;
vector<int> v2;
v2.reserve(10);
v2.push_back(1);
v2.push_back(2);
v2.push_back(3);
v2.push_back(4);
v2.push_back(5);
v2.resize(8, 8);
for (auto e : v2)
{cout << e << " ";}
cout << endl;
v2.resize(20, 20);
for (auto e : v2)
{cout << e << " ";}
cout << endl;
v2.resize(3);
for (auto e : v2)
{cout << e << " ";
}
cout << endl;
}
结论:如果对象中涉及到资源管理时,千万不能使用memcpy进行对象之间的拷贝,因为memcpy是浅拷贝,否则可能会引起内存泄漏甚至程序崩溃。
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];//T对象是自定义类型时,调用的对象是operator=
}
delete[] _start;
}
_start = tmp;
_finish = _start + sz;
_end_of_storage = _start + n;
}
}
//v2(v1)
vector(const vector<T>& v)
{
_start = new T[v.size()]; // 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();
_end_of_storage = _start + v.size();
}
拷贝数据:
- 拷贝构造
- 扩容