目录
1.vector常见接口及样例
1.1vector常见的构造接口及样例使用
(constructor)构造函数声明 接口说明
vector()(重点) 无参构造
vector(size_type n, const value_type& val = value_type()) 构造并初始化n个val
vector (const vector& x); (重点) 拷贝构造
vector (InputIterator first, InputIterator last); 使用迭代器进行初始化构造
#include <iostream>
#include <vector>
int main ()
{
std::vector<int> first;
// empty vector of ints
std::vector<int> second (4,100);
// four ints with value 100
std::vector<int> third (second.begin(),second.end());
// iterating through second
std::vector<int> fourth (third);
// a copy of third
return 0;
}
1.2 vector迭代器的使用及样例使用
iterator的使用 接口说明
begin+end(重点) 获取第一个数据位置的迭代器, 获取最后一个数据的 下一个位置的迭代器
rbegin + rend 获取最后一个数据位置的迭代器,获取第一个数据前一个 位置的迭代器
#include <iostream>
#include <vector>
using namespace std;
void PrintVector(const vector<int>& v)
{
// const对象使用const迭代器进行遍历打印
vector<int>::const_iterator it = v.begin();
while (it != v.end())
{
cout << *it << " ";
++it;
}
cout << endl;
}
1.3 vector空间增长常见接口及样例使用
容量空间 接口说明
size 获取数据个数
capacity 获取容量大小
empty 判断是否为空
resize(重点) 改变vector的size
reserve (重点) 改变vector的capacity
// vector::capacity
#include <iostream>
#include <vector>
int main ()
{
size_t sz;
std::vector<int> foo;
sz = foo.capacity();
std::cout << "making foo.capacity grow:\n";
for (int i=0; i<100; ++i) {
foo.push_back(i);//尾插元素
if (sz!=foo.capacity()) {
sz = foo.capacity();//当元素增加到超出capacity(容量)自动增容,将增容后容量赋值给sz
std::cout << "capacity changed: " << sz << '\n';//同时,输出当前容量
}
}
}
//reserve改变vector的capacity,提前扩容,避免使用时才扩容,增加效率
std:vector<int>bar;
sz = bar.capacity();
bar.reserve(100);
std::vector<int> myvector;//构建一个空的myvector
for (int i=1;i<10;i++){//前10位尾插整数1——10
myvector.push_back(i);
}
myvector.resize(5);//size变为5
myvector.resize(8,100);//size变为8,前八位初始化为100
myvector.resize(12);//size变为12
1.4vector的增删改查
vector增删查改 接口说明
push_back(重点) 尾插
pop_back (重点) 尾删
find 查找(注意这个是算法模块实现,不是vector的成员接口)
insert 在position之前插入val
erase 删除position位置的数据
swap 交换两个vector的数据空间
operator[] (重点) 像数组一样访问
// push_back/pop_back
#include <iostream>
#include <vector>
using namespace std;
int main()
{
int a[] = { 1, 2, 3, 4 };
vector<int> v(a, a+sizeof(a)/sizeof(int));//用数组构造vector
vector<int>::iterator it = v.begin();//迭代器打印数组
while (it != v.end()) {
cout << *it << " ";
++it;
}
cout << endl;
v.pop_back();
v.pop_back();//两次尾删
it = v.begin();
while (it != v.end()) {
cout << *it << " ";
++it;
}
cout << endl;
return 0;
}
// find / insert / erase
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
int main()
{
int a[] = { 1, 2, 3, 4 };
vector<int> v(a, a + sizeof(a) / sizeof(int));//用vector类模板构造出
// 使用find查找3所在位置的iterator
vector<int>::iterator pos = find(v.begin(), v.end(), 3);
// 在pos位置之前插入30
v.insert(pos, 30);
vector<int>::iterator it = v.begin();
while (it != v.end()) {
cout << *it << " ";
++it;
}
cout << endl;
pos = find(v.begin(), v.end(), 3);//find就是给首位,最后一位下一位迭代器+查找数值,进行查找
// 删除pos位置的数据
v.erase(pos);
it = v.begin();
while (it != v.end()) {
cout << *it << " ";
++it;
}
cout << endl;
return 0;
}
#include <iostream>
#include <vector>
using namespace std;
int main()
{
int a[] = { 1, 2, 3, 4 };
vector<int> v(a, a + sizeof(a) / sizeof(int));//构造v
vector<int> swapv;
swapv.swap(v);//交换两个类空间
return 0;
};
2.vector的底层实现
namespace zy
{
template<class T>
class vector
{
public:
// Vector的迭代器是一个原生指针
typedef T* iterator;
typedef const T* const_iterator;
iterator begin()
{
return _start;
}
iterator end()
{
return _finish;
}
const_iterator cbegin()
{
return _start;
}
const_iterator cend() const
{
return _finish;
}
// construct and destroy
vector()
:_start(nullptr)
,_finish(nullptr)
,_endOfStorage(nullptr)
{}
vector(int n, const T& value = T())
: _start(nullptr)
, _finish(nullptr)
, _endOfStorage(nullptr)
{
reserve(n);
while (n--)
{
push_back(value);
}
}
template<class InputIterator>
vector(InputIterator first, InputIterator last)
{
reserve(last - first);//开辟空间
while (first != last)//按位插入
{
push_back(*first);
++first;
}
}
vector(const vector<T>& v)//拷贝构造
: _start(nullptr)
, _finish(nullptr)
, _endOfStorage(nullptr)
{
reserve(v.capacity());
iterator it = begin();
const_iterator vit = v.cbegin();
while (vit != v.cend())//遍历赋值
{
*it++ = *vit++;
}
}
vector<T>& operator= (vector<T> v)
{
swap(v);
return *this;
}
~vector()
{
delete[] _start;
_start = _finish = _endOfStorage = nullptr;
}
// capacity
size_t size() const
{
return _finish - _start;
}
size_t capacity() const
{
return _endOfStorage - _start;
}
void reserve(size_t n)
{
if (n > capacity())
{
size_t oldSize = size();
T* tmp = new T[n];
if (_start)
{
for (size_t i = 0; i < oldSize; ++i)
tmp[i] = _start[i];
}
_start = tmp;
_finish = _start + oldSize;
_endOfStorage = _start + n;
}
}
void resize(size_t n, const T& value = T())
{
// 1.如果n小于当前的size,则数据个数缩小到n
if (n <= size())
{
_finish = _start + n;
return;
}
// 2.空间不够则增容
if (n > capacity())
reserve(n);
// 3.将size扩大到n
iterator it = _finish;
_finish = _start + n;
while (it != _finish)
{
*it = value;
++it;
}
}
///////////////access///////////////////////////////
T& operator[](size_t pos){ return _start[pos]; }
const T& operator[](size_t pos)const{ return _start[pos]; }
///////////////modify/////////////////////////////
void push_back(const T& x){ insert(end(), x); }
void pop_back(){ erase(--end()); }
void swap(vector<T>& v)
{
std::swap(_start, v._start);
std::swap(_finish, v._finish);
std::swap(_endOfStorage, v._endOfStorage);
}
iterator insert(iterator pos, const T& x)
{
assert(pos <= _finish);
// 空间不够先进行增容
if (_finish == _endOfStorage)
{
//size_t size = size();
size_t newCapacity = (0 == capacity()) ? 1 : capacity() * 2;
reserve(newCapacity);
// 如果发生了增容,需要重置pos
pos = _start + size();
}
iterator end = _finish - 1;
while (end >= pos)
{
*(end + 1) = *end;
--end;
}
*pos = x;
++_finish;
return pos;
}
iterator erase(Iterator pos)
{
// 挪动数据进行删除
iterator begin = pos + 1;
while (begin != _finish) {
*(begin - 1) = *begin;
++begin;
}
--_finish;
return pos;
}
private:
iterator _start; // 指向数据块的开始
iterator _finish; // 指向有效数据的尾
iterator _endOfStorage; // 指向存储容量的尾
};
}