vector容器
动态的顺序表,数组。
vector操作
vector操作及其概念
构造
vector<int>v1;vector<int>v2(10, 5);vector<int>v3(v2);int array[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 0 };vector<int>v4(array, array + sizeof(array) / sizeof(array[0]));vector<int>v5{ 1, 2, 3, 4, 5, 6, 7, 8, 9, 0 };//c++11中给出的构造
容量
cout << v5.size() << endl;
cout << v5.capacity() << endl;
cout << v5.front() << endl;
cout << v5.back() << endl;
vector增容机制
size_t sz;
std::vector<int> foo;
sz = foo.capacity();
std::cout << "making foo grow:\n";
for (int i = 0; i<100; ++i)
{ foo.push_back(i); if (sz != foo.capacity()) { sz = foo.capacity(); std::cout << "容量改变: " << sz << '\n'; }
}
vs2013底层的vector增容机制差不多是1.5倍的方式进行扩容。
而linux下g++的vector是按照两倍的方式进行扩容
在采用push_back向vector中尾插元素期间,如果知道大概知道要放置多少个元素的情况下,可以通过reverse提前将空间开辟号,这样效率就高了。
注意reserve的下面这种情况
size_t sz;
std::vector<int> foo;
foo.reserve(100); //底层空间有了但是没有有效元素
foo[0] = 10; //这个式子左边就错了,不能访问
总结
- 通过
push_back
来进行插入------通过reserve
来进行开辟空间 - 通过
[]
运算符进行插入------通过resize
来进行开辟空间
元素访问
for (size_t i = 0; i < v2.size(); ++i)
{cout << v2[i]<<" " ;
}
cout << endl;for (auto e : v3)
{cout << e<<" ";
}
cout << endl;
元素修改
void TestVector2()
{vector<int>v5{ 1, 2, 3, 4, 5, 6, 7, 8, 9, 0 };cout << v5.size() << endl;cout << v5.capacity() << endl;cout << v5.front() << endl;cout << v5.back() << endl;v5.push_back(1);v5.push_back(2);v5.push_back(3);cout << v5.size() << endl;cout << v5.capacity() << endl;cout << v5.front() << endl;cout << v5.back() << endl;v5.erase(v5.begin());v5.erase(v5.end() - 1);//vector里没有find方法,要想删除最后一个元素,必须用系统给的算法函数findauto pos = find(v5.begin(), v5.end(), 5);if (pos != v5.end())v5.erase(pos);v5.clear();
}
迭代器
迭代器:类似指针的一种类型,类似是一种指针类型,可以将迭代器定义的对象当成是指针方式进行应用
作用:帮助用户**透明的(用户可以不用知道该容器的底层数据结构)**遍历数据
auto it1 = v4.begin();
while (it1 != v4.end())
{cout << *it1<<" ";it1++;
}
cout << endl;auto it2 = v5.rbegin();
while (it2 != v5.rend())
{cout << *it2 << " ";it2++;
}
cout << endl;
有些情况下我们需要二维数组
void TestVector3()
{vector<vector<int>>vv;//10 * 10----->6vv.resize(10); //已经有了10行,但每一行还没有空间//给每行设置元素for (size_t i = 0; i < 10; ++i){//每一行10个元素vv[i].resize(10,6);}for (int i = 0; i < 10; ++i){for (int j = 0; j < 10; ++j){cout << vv[i][j] << " ";}cout<<endl;}}
迭代器失效
迭代器失效:迭代器本质是指针,指针失效,指针指向了非法空间
vector<int>v{ 1, 2, 3, 4, 5, 6, 7, 8, 9, 0 };for (size_t i = 0; i < v.size(); ++i){cout << v[i] << " ";}cout << endl;for (auto& e : v){cout << e << " ";}cout << endl;//迭代器:类似指针的一种类型//类似是一种指针类型,可以将迭代器定义的对象当成是指针方式进行应用//作用:帮助用户透明的(用户可以不用知道该容器的底层数据结构)遍历数据auto it = v.begin();//往容器里多增加了一个元素就引起了代码崩溃v.push_back(1);//vector<int>::iterator it = v.begin();while (it != v.end()){cout << *it << " ";++it;}cout << endl;
扩容后,原来空间释放,it迭代器指向非法空间
vector:迭代器失效场景
- push_back----可以自动扩容
- resize
- reserve
- insert
- assign
还有一种场景
vector<int>v{ 1, 2, 3, 4, 5, 6, 7, 8, 9, 0 };//v.resize(0);//v.clear();//v.erase(v.begin(), v.end());auto it = v.begin();while (it!=v.end()){v.erase(it);++it;}cout << v.size() << endl;
因为
返回值指向了新的空间
如何解决
给当前迭代器重新赋值,让其指向有效空间
auto it = v.begin();//v.assign(20, 8);v.push_back(1); //可能会迭代器失效//解决方式:给it迭代器重新赋值it = v.begin();while (it != v.end()){cout << *it << " ";++it;}cout << endl;
引用失效
vector<int>v{ 1, 2, 3, 4, 5, 6, 7, 8, 9, 0 };int& ra = v[0];ra = 10;v.push_back(1);ra = 100;
杨辉三角
用二维数组,每一行用一个一维数组存。
class Solution {
public:vector<vector<int>> generate(int numRows) {vector<vector<int>> ret;ret.resize(numRows); //先创建numRows行for(size_t i=0;i<numRows;++i){ret[i].resize(i+1);ret[i][0] = 1;ret[i][i] = 1;}for(size_t i = 2; i < numRows; ++i){//j为每一列for(size_t j = 1; j < i; ++j){ret[i][j]=ret[i-1][j]+ret[i-1][j-1];}}return ret;}
};
vector模拟实现
vector结构
class vector
{
public:typedef T* iterator;
private:T* _start;T* _finish;T* _endofstorage;};
构造与销毁
// 构造与销毁vector():_start(nullptr), _finish(nullptr), _endofstorage(nullptr){}vector(int n, const T& data):_start(new T[n]){for (size_t i = 0; i < n; ++i){_start[i] = data;}_finish = _start + n;_endofstorage = _finish;}//[first,last)template<class Iterator>vector(Iterator first, Iterator last){//计算[first,last)区间中元素的个数size_t n = 0;auto it = first;while (it != last){++it;++n;}_start = new T[n];//将[first,last)区间中的元素放置到_start空间中for (size_t i = 0; i < n; ++i){_start[i] = *first++;}_finish = _start + n;_endofstorage = _start + n;}vector(const vector<T>& v);vector<T>& operator=(const T& v);~vector(){if (_start){delete[] _start;_start = _finish = _endofstorage = nullptr;}}
容量操作
// 容量操作
size_t size()const
{return _finish - _start;
}size_t capacity()const
{return _endofstorage - _start;
}bool empty()const
{return _start == _finish;
}// T():
// 如果T代表内置类型,T()--->0
// 如果T代表自定义类型,T()---> 调用该类的无参构造函数
void resize(size_t newsize, const T& data = T())
{size_t oldsize = size();if (newsize > oldsize){size_t cap = capacity();if (newsize > cap)reserve(newsize);for (; oldsize < newsize; ++oldsize)_start[oldsize] = data;//*_finish++ = data;}_finish = _start + newsize;
}void reserve(size_t newCapacity)
{size_t oldCapcity = capacity();if (newCapacity > oldCapcity){// 1. 申请新空间T* temp = new T[newCapacity];// 2. 拷贝元素//memcpy(temp, _start, size()*sizeof(T));// 如果_start指向的空间存在size_t n = size();if (_start){for (size_t i = 0; i < n; ++i){temp[i] = _start[i];}// 3. 释放旧空间delete[] _start;}_start = temp;_finish = _start + n;_endofstorage = _start + newCapacity;}
}
元素访问操作
//元素访问的操作//v[0] = 100; v如果是普通类型T& operator[](size_t index){assert(index <= size());return _start[index];}//对于const类型的变量const T& operator[](size_t index)const{assert(index <= size());return _start[index];}T& front(){return *_start;}const T& front()const{return *_start;}T& back(){return *(_finish-1);}const T& back()const{return *(_finish-1);}
元素修改
//元素修改
void push_back(const T& data)
{//检测是否需要扩容if (_finish == _endofstorage){reserve(capacity() * 2 + 3);}*_finish++ = data;
}void pop_back()
{--_finish;
}//返回值含义:反回新插入元素的位置
iterator insert(iterator pos, const T&data)
{//检测是否需要扩容if (_finish == _endofstorage){reserve(capacity() * 2 + 3);}//插入元素//将[pos,finish)之间所有元素整体向后搬移一个位置auto it = _finish;while (it > pos){*it = *(it - 1);it--;}//插入新元素*pos = data;_finish++;return pos;
}iterator erase(iterator pos)
{if (pos == end())return pos;//it代表待搬移元素的位置auto it = pos + 1;while (it != _finish){*(it - 1) = *it;++ot;}_finish--;return pos;
}
迭代器操作
iterator begin()
{return _start;
}iterator end()
{return _finish;
}
测试代码
void TestVector()
{bite::vector<int>v1;bite::vector<int>v2(10, 5);int array[] = { 1, 2, 3, 4, 5 };bite::vector<int>v3(array,array+sizeof(array)/sizeof(array[0]));cout << v2.size() << endl;cout << v2.capacity() << endl;cout << v3.front() << endl;cout << v3.back() << endl;for (size_t i = 0; i < v3.size(); ++i)cout << v3[i] << " ";cout << endl;//bite::vector<int>::iterator it = v3.begin();auto it = v3.begin();while (it != v3.end()){cout << *it << " ";++it;}cout << endl;for (auto& e : v3)e *= 2;for (auto e : v3)cout << e << " ";cout << endl;
}
void TestVector2()
{bite::vector<int>v;v.push_back(1);v.push_back(2);v.push_back(3);v.push_back(4);cout << v.size() << endl;cout << v.capacity() << endl;cout << v.back() << endl;v.pop_back();cout << v.back() << endl;cout << v.size() << endl;cout << v.front() << endl;v.insert(v.begin(), 0);cout << v.front() << endl;cout << v.size() << endl;}
void TestVector3()
{bite::vector<int>v(10, 5);cout << v.size() << endl;cout << v.capacity() << endl;v.resize(5);cout << v.size() << endl;cout << v.capacity() << endl;v.resize(8);cout << v.size() << endl;cout << v.capacity() << endl;v.resize(20, 5);cout << v.size() << endl;cout << v.capacity() << endl;
}