文章目录
- 一、vector常用接口介绍
- 1、initializer_list
- 2、接口有很多类似
- 3、typeid(类型).name()
- 4、find() 函数
- 5、内置类型构造
- 二、vector()常用接口模拟实现
截图来源网站:https://legacy.cplusplus.com/reference/vector/vector/
一、vector常用接口介绍
是一个类模版,可以创建各种类型的数组。
1、initializer_list
可以支持初始化多个数据
//initializer_list可以实现大括号初始化vector<int> v1 = { 1,2,3,4,5 };vector<int> v2({ 1,2,3,4,5 });//可以遍历无法修改auto i1 = { 1,2,3,4,5 };initializer_list<int> i2 = { 1,2,3,4,5 };//类似这个数组int a[] = { 1,2,3,4,5 };
2、接口有很多类似
- size() 获取有效数据个数
- begin() , end() 迭代器
- capacity()空间大小
- empty()判段是否为空
- reserve()对空间进行扩容
- resize()对有效数据进行调整,一般空间不够会扩容,但不会缩容空间
- shrink_to_fit()对空间缩容跟size一样大小
- operator[]越界是直接断言
- at()越界是抛异常
- date()返回底层数组的指针
- assign()是一直赋值,会覆盖原来的数据,空间不够会扩容
- push_back()区别是只能单个数据的插入
- insert()插入数据但不支持下标的插入,支持迭代器区间
- erase()删除一个值这个值的位置是迭代器,删除一端迭代器区间
- swap()交换
- emplace()跟insert()功能一样
- emplace_back()跟push_back()功能一样,有的场景效率更高
3、typeid(类型).name()
可以拿到一个真实的类型
vector里面的迭代器可以看到实现的很复杂
4、find() 函数
不是vector的函数成员,是库里面的一个通用函数模版
给段段左闭右开的迭代器区间寻找指定的值,找到返回对应位置的迭代器,没找到返回last迭代器
5、内置类型构造
C++对内置类型也进行了升级
int i = int();int k = int(10);int j(100);
二、vector()常用接口模拟实现
#pragma once#include <iostream>
#include <assert.h>
#include <string.h>
#include <vector>
#include <algorithm>
#include <initializer_list>
#include <string>namespace lsh
{template<class T>class vector{public:typedef T* iterator;typedef const T* const_iterator;iterator begin(){return _start;}const_iterator begin() const{return _start;}iterator end(){return _finish;}const_iterator end() const{return _finish;}vector():_start(nullptr),_finish(nullptr),_end_of_storage(nullptr){}vector(std::initializer_list<T> li):_start(nullptr), _finish(nullptr), _end_of_storage(nullptr){reserve(li.size());for (auto& c : li){push_back(c);}}vector(const vector<T>& v){reserve(v.capacity());for (auto& c : v){push_back(c);}}template<class InputIterator>vector(InputIterator first, InputIterator last){while (first != last){push_back(*first);first++;}}vector(size_t n, T val = T()){reserve(n);for (size_t i = 0; i < n; i++){push_back(val);}}vector(int n, T val = T()){reserve(n);for (size_t i = 0; i < n; i++){push_back(val);}}~vector(){if (_start){delete[] _start;_start = _finish = _end_of_storage = nullptr;}}void resize(size_t n, T val = T()){if (n < size()){_finish = _start + n;}else{reserve(n);while (_finish < _start + n){*_finish = val;_finish++;}}}size_t size()const{return _finish - _start;}size_t capacity()const{return _end_of_storage - _start;}void reserve(size_t n){size_t s = size();if (n > capacity()){T* tmp = new T[n];if (_start){for (int i = 0; i < s; i++){tmp[i] = _start[i];}delete [] _start;}_start = tmp;_finish = _start + s;_end_of_storage = _start + n;}}void push_back(const T& x){if (_finish == _end_of_storage){reserve(capacity() == 0 ? 4 : capacity() * 2);}*_finish = x;++_finish;}void swap(vector<T> v){std::swap(_start, v._start);std::swap(_finish, v._finish);std::swap(_end_of_storage, v._end_of_storage);}vector<T>& operator=(vector<T> v){swap(v);return *this;}T& operator[](size_t n){assert(n < size());return _start[n];}const T& operator[](size_t n) const{assert(n < size());return _start[n];}bool empty(){return _start == _finish;}void pop_back(){assert(_finish > _start);--_finish;}//存在迭代器失效,要返回更新后的迭代器iterator insert(iterator pos, const T& x){assert(pos >= _start && pos <= _finish);if (_finish == _end_of_storage){int n = pos - _start;reserve(capacity() == 0 ? 4 : capacity() * 2);pos = _start + n;}iterator i = _finish;while (i > pos){*i = *(i-1);i--;}*pos = x;++_finish;return pos;}iterator erase(iterator pos){assert(pos >= _start && pos < _finish);iterator i = pos;while (i < _finish -1){*i = *(i + 1);i++;}--_finish;return pos;}private:iterator _start = nullptr;iterator _finish = nullptr;iterator _end_of_storage = nullptr;};
}