新号别搞:vector


1. vector使用


1.1 vector 链接

cplusplus.com/reference/vector/vector/https://cplusplus.com/reference/vector/vector/


1.2 vector的定义


1.3 vector iterator 的使用


1.4 vector 空间增长问题

  • vs下capacity是按1.5倍增长的,g++是按2倍增长的,但具体增长多少是根据具体的需求定义的
  • resize在开空间的同时还会进行初始化,影响size

1.5 vector 增删查改


1.6 迭代器失效

  • 物理空间被销毁:扩容后旧内存被释放,迭代器指向的是一块已经不属于容器的地址。这是最危险的情况,解引用可能直接崩溃。
  • 逻辑位置错位:内存没释放,但元素发生了搬移,迭代器指向的地址还在,可里面存的内容已经不是它"以为"的那个元素了。这种情况更隐蔽,程序不崩溃但结果错误。
  • 任何可能使迭代器失效的操作之后,立即重新获取迭代器,绝不复用旧的

2. vector模拟实现


2.1 类部分

cpp 复制代码
template<class T>
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)
		,_end_of_storage(nullptr){}

	T& operator[](size_t pos) {
		assert(pos < size());
		return _start[pos];
	}
	const T& operator[](size_t pos)const {
		assert(pos < size());
		return _start[pos];
	}

	void reserve(size_t n);
	void resize(size_t n, const T& val);

	void push_back(const T& x);
	void pop_back();
	size_t capacity()const { return _end_of_storage - _start;}
	size_t size()const { return _finish - _start; }
	bool empty()const { return _start == _finish; }

	void insert(iterator pos, const T& x);
	iterator erase(iterator pos);

private:
	iterator _start;
	iterator _finish;
	iterator _end_of_storage;
};

2.2 接口实现

cpp 复制代码
template<class T>
void vector<T>::push_back(const T& x) {
	//判断扩容
	if (_finish == _end_of_storage) {
		size_t newcapacity = capacity() == 0 ? 4 : capacity() * 2;
		reserve(newcapacity);
	}
	*_finish = x;
	++_finish;
}
template<class T>
void vector<T>::pop_back() {
	assert(!empty());
	--_finish;
}
template<class T>
void vector<T>::reserve(size_t n) {
	//不考虑缩容(代价大),不能部分空间释放
	if (n > capacity()) {
		size_t old_size = size();
		T* tmp = new T[n];
		if (_start) {
			memcpy(tmp, _start, old_size * sizeof(T));
			delete[]_start;
		}
		_start = tmp;
		_finish = _start + old_size;
		_end_of_storage = _start + n;
	}
}
template<class T>
void vector<T>::insert(iterator pos, const T& x) {
	assert(pos >= _start);
	assert(pos <= _finish);
	//扩容,迭代器失效
	if (_finish == _end_of_storage) {
		size_t len = pos - _start;
		size_t newcapacity = capacity() == 0 ? 4 : capacity() * 2;
		reserve(newcapacity);
		pos = _start + len;//扩容后更新pos,解决Pos失效问题
	}
	memmove(pos + 1, pos, sizeof(T) * (_finish - pos));
	*pos = x;
	++_finish;
}
template<class T>
typename vector<T>::iterator vector<T>::erase(iterator pos) {
	assert(pos >= _start);
	assert(pos < _finish);
	iterator it = pos + 1;
	while (it < _finish) {
		*(it - 1) = *it;
		++it;
	}
	--_finish;
	return pos;
}

相关推荐
CodeYp20195 小时前
第15讲:二叉搜索树
开发语言·c++·二叉搜索树
pipixia200336 小时前
C++继承面试四连问:内存叠加、私有继承、sizeof、权限重映射
c++·面试·嵌入式·继承·面向对象·对象模型
牵猫散步的鱼儿9 小时前
C++ 内存分区
c++
choumin9 小时前
C++ 标准容器为什么不能存引用
c++·容器·引用类型
谙弆悕博士9 小时前
用 C 写了个 Windows 聊天室,踩了几个多线程和控制台的坑
c语言·开发语言·c++·windows·网络编程·聊天室
stolentime10 小时前
CF2277E Tree Guessing题解
c++·算法·贪心算法·交互·ad-hoc
syagain_zsx11 小时前
ODB 与 MySQL:C++ 里的 ORM 实践(类即表、事务铁律与查询封装)
开发语言·数据库·c++·mysql·工具库·1024程序员节
小鱼仙官11 小时前
使用Qt封装C/C++ 动态库 用于第三方调用
c语言·c++·qt
晚风予卿云月12 小时前
【C++高并发内存池】 1.定长内存池的实现
开发语言·c++·内存管理
闻缺陷则喜何志丹13 小时前
【c++20】协程与屏障
c++·算法·c++20·携程·屏障