- vector<int> v1;//无参构造
- 8 vector<int> v2(5,55);//构造有5个55的vector
- 9 vector<int> v3(v2);//拷贝构造
- 10 vector<int> v4(v3.begin(),v3.end());//迭代器区间构造
begin()/end() 获取第一个数据位置的iterator/const_iterator,获取最后一个位置下一个的iterator/const_iterator
rbegin()/rend() 获取最后一个数据位置的iterator/const_iterator,获取第一个位置前一个的iterator/const_iterator
- vector<int>:: iterator it1 = v2.begin();
- 32 while(it1!=v2.end())
- 33 {
- 34 cout << *it1 << " ";
- 35 it1++;
- 36 }
- 37 cout << endl;
- 38 const vector<int> v5;
- 39 vector<int>::const_iterator it2 = v5.begin();
- 40 while(it2!=v5.end())
- 41 {
- 42 cout << *it2 << " ";
- 43 it2++;
- 44 }
- 45 cout << endl;
- 46 vector<int>:: reverse_iterator it3 = v2.rbegin();
- 47 while(it3!=v2.rend())
- 48 {
- 49 cout << *it3 << " ";
- 50 it3++;
- 51 }
- 52 cout << endl;
- 53 const vector<int> v6;
- 54 vector<int>::const_reverse_iterator it4 = v6.rbegin();
- 55 while(it4!=v6.rend())
- 56 {
- 57 cout << *it4 << " ";
- 58 it4++;
- 59 }
- 60 cout << endl;
- cout << v2.size() << endl;
- cout << v2.capacity() << endl;
- cout << v2.empty() << endl;
- v2.reserve(100);
- cout << v2.size() << endl;
- cout << v2.capacity() << endl;
- v2.resize(10,6);
- cout << v2.size() << endl;
- cout << v2.capacity() << endl;
- vector<int> vec;
- vec.push_back(1);
- vec.push_back(2);
- vec.push_back(3);
- vec.pop_back();
- vec.insert(vec.end(),10);
- vec.insert(vec.end(),20);
- vec.insert(vec.end(),30);
- vec.erase(vec.begin());
- for(size_t i=0;i
size();i++) - {
- cout << vec[i] << " ";
- }
- cout << endl;
- #include
- #include
- #include
- using namespace std;
-
- namespace hj
- {
- template<class T>//创建一个模板类vector
- 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()
- :_start(nullptr),
- _finish(nullptr),
- _en_of_storage(nullptr)
- {}
- //析构
- ~vector()
- {
- delete[] _start;
- _start = _finish = _en_of_storage;
- }
- //迭代器区间构造
- template <class InputIterator>
- vector(InputIterator first, InputIterator last)
- :_start(nullptr),
- _finish(nullptr),
- _en_of_storage(nullptr)
- {
-
- while(first != last)
- {
- push_back(*first);
- ++first;
- }
- }
- void swap(vector
& v) //交换两个vector的值 - {
- std::swap(v._start,_start);
- std::swap(v._finish,_finish);
- std::swap(v._en_of_storage,_en_of_storage);
- }
- //拷贝构造-深拷贝
- vector(const vector
& v) - :_start(nullptr),
- _finish(nullptr),
- _en_of_storage(nullptr)
- {
- vector
tmp(v.begin(),v.end()) ; - swap(tmp);
- }
- //赋值重载
- vector
& operator=(vector v) - {
- swap(v);
- return *this;
- }
- size_t size()
- {
- return _finish - _start;
- }
- size_t capacity()
- {
- return _en_of_storage - _start;
- }
- void reserve(size_t n)//扩容
- {
- if(n > capacity())
- {
- size_t len = size();
- T* tmp = new T[n];
- if(_start)//判断是否为空
- {
-
- for(size_t i = 0;i < len; i++)//拷贝数据
- {
- tmp[i] = _start[i];
- }
- delete[] _start;
- }
- _start = tmp;
- _finish = _start + len;
- _en_of_storage = _start + n;
- }
- }
- void resize(size_t n,T& val=T())//设置有效数据个数
- {
- if(n>capacity())//扩容
- {
- reserve(n);
- }
- if(n>size())
- {
- while(_finish < _start + n)
- {
- *_finish = val;
- ++_finish;
- }
- }
- else
- {
- _finish = _start + n;
- }
- }
- void push_back(const T& val)
- {
- insert(_finish,val);
- }
- void pop_back()
- {
- erase(_finish-1);
- }
- iterator insert(iterator pos,const T& val)
- {
- assert(pos >=_start);
- assert(pos <=_finish);
- if(_finish == _en_of_storage)//判断扩容
- {
- size_t len = pos - _start;
- reserve(capacity()==0?4:capacity()*2);
- pos = _start + len;//更新扩容后pos的位置
- }
- iterator end = _finish-1;
- while(end >= pos)
- {
- *(end+1) = *(end);//用end = end-1在pos为_start时会越界
- end--;
- }
- *pos = val;
- _finish++;
- return pos;
- }
-
- iterator erase(iterator pos)
- {
- assert(pos >= _start);
- assert(pos < _finish);
- iterator begin = pos+1;
- while(begin < _finish)
- {
- *(begin-1) = *begin;
- begin++;
- }
- _finish--;
- return pos;
- }
- T& operator[](size_t sz)
- {
- assert(sz < size());
- return *(_start + sz);
- }
- const T& operator[](size_t sz)const
- {
- assert(sz < size());
- return *(_start + sz);
- }
-
- T& front()
- {
- assert(size()>0);
- return *_start;
- }
- T& back()
- {
- assert(size()>0);
- return *(_finish-1);
- }
- private:
- iterator _start;
- iterator _finish;
- iterator _en_of_storage;
- };
- void test_vector1()//测试基本功能
- {
- vector<int> v1;//构造
- v1.push_back(1);
- v1.push_back(2);
- v1.push_back(3);
- v1.push_back(4);
- v1.push_back(5);
- vector<int> v2(v1);//拷贝构造
- for(auto ch : v1)//迭代器
- {
- cout << ch << " ";
- }
- cout << endl;
- for(auto ch : v2)
- {
- cout << ch << " ";
- }
- cout << endl;
- for(size_t i=0;i
size();i++)//[]重载 - {
- cout << v1[i] << " ";
- }
- cout << endl;
- vector<int> v3;
- v3=v1;//赋值重载
-
- for(auto ch : v3)
- {
- cout << ch << " ";
- }
- cout << endl;
-
- v3.erase(v3.begin());//删除
- v3.erase(v3.end()-1);
- for(auto ch : v3)
- {
- cout << ch << " ";
- }
- cout << endl;
-
- cout<< "size->"<
size()< - cout<< "capacity->"<
capacity()< -
-
- }
- }
-
相关阅读:
【Vue】06 计算属性 侦听属性
城市物流管理系统的设计与实现
C++拿几道题练练手吧
python笔记(input、print、多重判断、两个循环(九九乘法表))
【JavaSE】基础笔记 - 类和对象(下)
408计网应用层总结
解决 Android 依赖冲突
2022面试笔记
MySQL8.0安装教程,在Linux环境安装MySQL8.0教程,最新教程 超详细
LeetCode 141. 环形链表
-
原文地址:https://blog.csdn.net/qq_64105689/article/details/126913435