一、什么是vector?
向量(Vector)是一个封装了动态大小数组的顺序容器(Sequence Container)。跟任意其它类型容器一样,它能够存放各种类型的对象。可以简单的认为,向量是一个能够存放任意类型的动态数组。
二、容器特性
1.顺序序列
顺序容器中的元素按照严格的线性顺序排序。可以通过元素在序列中的位置访问对应的元素。
2.动态数组
支持对序列中的任意元素进行快速直接访问,甚至可以通过指针算述进行该操作。操供了在序列末尾相对快速地添加/删除元素的操作。
3.能够感知内存分配器的(Allocator-aware)
容器使用一个内存分配器对象来动态地处理它的存储需求。
三、基本函数实现
1.构造函数
- vector():创建一个空vector
- vector(int nSize):创建一个vector,元素个数为nSize
- vector(int nSize,const t& t):创建一个vector,元素个数为nSize,且值均为t
- vector(const vector&):复制构造函数
- vector(begin,end):复制[begin,end)区间内另一个数组的元素到vector中
2.增加函数
- void push_back(const T& x):向量尾部增加一个元素X
- iterator insert(iterator it,const T& x):向量中迭代器指向元素前增加一个元素x
- iterator insert(iterator it,int n,const T& x):向量中迭代器指向元素前增加n个相同的元素x
- iterator insert(iterator it,const_iterator first,const_iterator last):向量中迭代器指向元素前插入另一个相同类型向量的[first,last)间的数据
3.删除函数
- iterator erase(iterator it):删除向量中迭代器指向元素
- iterator erase(iterator first,iterator last):删除向量中[first,last)中元素
- void pop_back():删除向量中最后一个元素
- void clear():清空向量中所有元素
4.遍历函数
- reference at(int pos):返回pos位置元素的引用
- reference front():返回首元素的引用
- reference back():返回尾元素的引用
- iterator begin():返回向量头指针,指向第一个元素
- iterator end():返回向量尾指针,指向向量最后一个元素的下一个位置
- reverse_iterator rbegin():反向迭代器,指向最后一个元素
- reverse_iterator rend():反向迭代器,指向第一个元素之前的位置
5.判断函数
- bool empty() const:判断向量是否为空,若为空,则向量中无元素
6.大小函数
- int size() const:返回向量中元素的个数
- int capacity() const:返回当前向量张红所能容纳的最大元素值
- int max_size() const:返回最大可允许的vector元素数量值
7.其他函数
- void swap(vector&):交换两个同类型向量的数据
- void assign(int n,const T& x):设置向量中第n个元素的值为x
- void assign(const_iterator first,const_iterator last):向量中[first,last)中元素设置成当前向量元素
8.看着清楚
1.push_back 在数组的最后添加一个数据
2.pop_back 去掉数组的最后一个数据
3.at 得到编号位置的数据
4.begin 得到数组头的指针
5.end 得到数组的最后一个单元+1的指针
6.front 得到数组头的引用
7.back 得到数组的最后一个单元的引用
8.max_size 得到vector最大可以是多大
9.capacity 当前vector分配的大小
10.size 当前使用数据的大小
11.resize 改变当前使用数据的大小,如果它比当前使用的大,者填充默认值
12.reserve 改变当前vecotr所分配空间的大小
13.erase 删除指针指向的数据项
14.clear 清空当前的vector
15.rbegin 将vector反转后的开始指针返回(其实就是原来的end-1)
16.rend 将vector反转构的结束指针返回(其实就是原来的begin-1)
17.empty 判断vector是否为空
18.swap 与另一个vector交换数据
#include <iostream> #include <vector> #include <algorithm>//算法库 using namespace std; void main() { vector<int> vec1;//初始化一个向量 vector<int> vec2(5);//初始化一个大小为5的向量 vec2 = { 10,20,30,40,50 };//赋值 vector<int> vec3(5,1);//初始化一个大小为5初值为1的向量 vector<int> vec4=vec3;//拷贝构造函数,且是深拷贝 vector<int> vec5(vec3);//拷贝构造函数,且是深拷贝 int arr[5] = { 1,2,3,4,5 }; vector<int> vec6(arr,arr+5);//迭代器+1 vector<int> vec7(&arr[0], &arr[5]);// vec1.push_back(10);//末尾添加元素 vec1.push_back(20); vec1.pop_back();//末尾删除元素 cout<<*vec1.begin()<<endl;//开始迭代器 //指向向量中最开始元素的指针 cout << *(vec1.end() - 1) << endl;//末尾迭代器 //指向向量中最后一个元素的下一个位置,要指向最后一个元素需-1 vec1.cbegin();//指向常量的迭代器(不能改值) 开始 vec1.cend();//指向常量的迭代器 结尾 vec1[1] = 90;//向量的本质是数组 下标访问不会检测越界 vec1.at(1)=88;//会检测越界 cout << vec1.front() << endl; //成员函数访问第一个元素,不会检测元素是否存在 cout << vec1.back() << endl;//成员函数访问最后一个元素,不会检测元素是否存在 int* p = vec1.data();//返回指向数组的指针 C++11 vec1.clear();//清空向量 for (int i = 0; i < 10; i++) { vec1.push_back(i); } //使用迭代器遍历 //vector<int>::iterator iter; for (auto iter = vec1.begin(); iter != vec1.end(); ++iter) { cout << *iter << endl; } cout << "元素个数:" << vec1.size() << endl; cout << "实际容量:" << vec1.capacity() << endl; reverse(vec1.begin(), vec1.end()); //元素翻转 算法库函数 reverse(vec1.begin(), vec1.begin()+2); //元素翻转 sort(vec1.begin(), vec1.end());//升序排序 算法库函数 vec2.swap(vec1);//交换 vec1.empty();//判断是否为空 vec3.reserve(10);//扩容 vec3.assign(3, 4);//重新确定存放数量3和赋值为4 vec3.resize(12, 8);//扩容,扩容的部分赋值为8,原来的值不变 system("pause"); return; }
四、自写函数实现
#pragma once template <typename T> class MyVector { T *pBuff; //动态数组首地址指针 size_t nSize; //动态数组元素个数 size_t nCapacity; //动态数组最大容量 public: typedef T* iterator;//普通迭代器 typedef const T* const_iterator;//常迭代器 iterator begin() { return pBuff; } iterator end() { return pBuff + nSize; } const_iterator begin() const { return pBuff; } const_iterator end() const{ return pBuff + nSize; } public: explicit MyVector(int size = 0);//构造一个容器,元素个数为size MyVector(int size, T const&; elem);//构造一个容器,元素个数为size,元素值为elem MyVector(MyVector<T> const& that);//拷贝构造 构造一个容器,元素是另一个容器 ~MyVector(); public: MyVector<T>& operator=(MyVector<T> const& that);//赋值重载 bool operator==(MyVector<T> const& that) const;//==重载 bool operator!=(MyVector<T> const& that) const;//!=重载 void clear(); //释放内存 void assign(int n, T const& elem);//复制n个elem 赋值给this void swap(MyVector<T>& that); friend void swap(MyVector<T>& v1, MyVector<T>& v2) { v1.swap(v2); } void reserve(int n);//扩充数组容量,不可以改小 void resize(int n);//改变元素个数 改大用构造改小用nsize=n void resize(int n, const T& elem); size_t size() const;//返回元素个数 size_t capacity() const;//返回向量容量 bool empty() const;// T& at(int index) const; T& operator[]int(int index) const; T& front(); T& back(); void push_back(T const& elem); void pop_back(); }; //构造一个容器,元素个数为size template<typename T> inline MyVector<T>::MyVector(int size):nSize(size),nCapacity(size),pBuff(new T[size]) { //pBuff = new T[size]; memset(pBuff, 0, sozeof(T)*size); //nSize = size; //nCapacity = size; } template<typename T> inline MyVector<T>::MyVector(int size, T const& elem) :nSize(size), nCapacity(size), pBuff(new T[size]) { for (int i = 0; i < size; ++i) { pBuff[i] = elem; } } template<typename T> inline MyVector<T>::MyVector(MyVector<T> const& that):pBuff(nullptr) { //拷贝构造 this->nSize = that.nSize; this->nCapacity = that.nCapacity; if (nCapacity != 0) { pBuff = new T[nCapacity]; memcpy(pBuff, that.pBuff, sizeof(T)*that.nSize); } //else pBuff = nullptr; } template<typename T> inline MyVector<T>::~MyVector() { clear(); } template<typename T> inline MyVector<T>& MyVector<T>::operator=(MyVector<T> const& that) { if (this != &that)//避免自赋值 { T* p_tmp = new T[that.nCapacity]; memcpy(p_tmp, that.pBuff, sizeof(T)*that.nSize); this.clear();//释放旧资源 this->pBuff = p_tmp; this->nSize = that.nSize; this->nCapacity = that.nCapacity; that.clear(); } return *this; } template<typename T> inline bool MyVector<T>::operator==(MyVector<T> const& that) const { if (nSize != that.nSize)//先判断数量 return false; for (size_t i = 0; i < nSize; ++i) { if (pBuff[i] != that.pBuff[i]) return false; } return true; } template<typename T> inline bool MyVector<T>::operator!=(MyVector<T> const& that) const { return !(*this==that) } template<typename T> inline void MyVector<T>::clear() { if (pBuff) { delete[] pBuff; } pBuff = nullptr; nSize = 0; } template<typename T> inline void MyVector<T>::assign(int n, T const& elem) { clear(); nSize = nCapacity = n; pBuff = new T[n]; for (size_t i = 0; i < n; ++i) { pBuff[i] = elem; } } template<typename T> inline void MyVector<T>::swap(MyVector<T>& that) { MyVector<T> temp; tmep = *this; *this = that; that = temp; } template<typename T> inline void MyVector<T>::reserve(int n) { if (n > nCapacity) { T* temp = new T[n]; memcpy(temp, pBuff, sizeof(T)*nSize); if (pBuff) delete[] pBuff;//释放旧内存 pBuff = temp;//接管新内存 nCapacity = n; } } template<typename T> inline void MyVector<T>::resize(int n) { if (n > nSize) { reserve(n); memset(&pBuff[nSize], 0, sizeof(T)*(n - nSize)); } else { nSize = n; } } template<typename T> inline void MyVector<T>::resize(int n, const T & elem) { if (n > nSize) { reserve(n); memset(&pBuff[nSize], elem, sizeof(T)*(n - nSize)); } else { nSize = n; } } template<typename T> inline size_t MyVector<T>::size() const { return nSize; } template<typename T> inline size_t MyVector<T>::capacity() const { return nCapacity; } template<typename T> inline bool MyVector<T>::empty() const { return nSize == 0; } template<typename T> inline T & MyVector<T>::at(int index) const { if (index >= 0 && index < nSize) return pBuff[index]; throw "out of range"//抛出异常 } template<typename T> inline T & MyVector<T>::operator[]int(int index) const { return pBuff[index]; } template<typename T> inline T & MyVector<T>::front() { return pBuff[0]; } template<typename T> inline T & MyVector<T>::back() { return pBuff[nSize - 1]; } template<typename T> inline void MyVector<T>::push_back(T const & elem) { if (nSize == nCapacity) { //容量满了 reserve(2 * nCapacity + 1); //+1是为了防止原来的容量是0 } PBuff[nSize++] = elem; } template<typename T> inline void MyVector<T>::pop_back() { if (nSize > 0) { nSize--; } }