vector实现
typedef int size_type;
/*
vector维护的是一个连续线性空间,提供的迭代器是Random Access Iterators即普通指针
*/
template <class T,class Alloc=alloc>
class vector
{
protected:
iterator start;//目前使用空间的头
iterator finish;//目前使用空间的尾
iterator end_of_storage;//目前可用空间的尾
public:
void push_back(const T& x);
void insert_aux(iterator position, const T& x);
int size()const{ return int(end() - begin()); }
}; /*
vector动态增加大小,并不是在原空间之后开辟新空间,因为无法保证原空间之后尚有可供配置的空间,而是以
原大小的两倍另外配置一块较大空间,然后将原内容拷贝过来,然后才开始在原内容之后构造新元素,并释放原空间
*/
template<class T,class Alloc=alloc>
void vector::push_back(const T& x)
{
if (finish != end_of_storage)
{
construct(finish, x);
++finish;
}
else//已无备用空间
insert_aux(end(), x);
} template<class T, class Alloc = alloc>
void vector<T, Alloc>::insert_aux(iterator position, const T& x)
{
const size_type old_size = size();
const size_type len = old_size != ? * old_size : ;
/*
配置原则:若原空间大小为0,则配置一个元素大小,否则配置原大小的两倍
前半段用来放置原数据,后半段用来放置新数据
*/
//data_allocator为vector专有的空间配置器
iterator new_start = data_allocator::allocate(len);
iterator new_finish = new_start;
//将原vector的内容拷贝到新vector
new_finish = uninitialized_copy(start, position, new_start);
//为新元素设定初值x
construct(new_finish, x);
++new_finish; //析构并释放原vector
destroy(begin(), end());
deallocate(); //调整迭代器
start = new_start;
finish = new_finish;
end_of_storage = new_start + len;
}
vector实现的更多相关文章
- c++ vector 使用
1. 包含一个头文件: 1 #include <vector> 2. 申明及初始化: std::vector<int> first; // empty vector of in ...
- Vector Tile
Mapbox Vector Tile Specification A specification for encoding tiled vector data. <?XML:NAMESPACE ...
- ArrayList、Vector、LinkedList的区别联系?
1.ArrayList.Vector.LinkedList类都是java.util包中,均为可伸缩数组. 2.ArrayList和Vector底层都是数组实现的,所以,索引数据快,删除.插入数据慢. ...
- ArrayList、Vector、HashMap、HashSet的默认初始容量、加载因子、扩容增量
当底层实现涉及到扩容时,容器或重新分配一段更大的连续内存(如果是离散分配则不需要重新分配,离散分配都是插入新元素时动态分配内存),要将容器原来的数据全部复制到新的内存上,这无疑使效率大大降低. 加载因 ...
- Java中Vector和ArrayList的区别
首先看这两类都实现List接口,而List接口一共有三个实现类,分别是ArrayList.Vector和LinkedList.List用于存放多个元素,能够维护元素的次序,并且允许元素的重复.3个具体 ...
- C++使用vector
#include <iostream> #include <string> #include <vector> using namespace std; void ...
- [LeetCode] Flatten 2D Vector 压平二维向量
Implement an iterator to flatten a 2d vector. For example,Given 2d vector = [ [1,2], [3], [4,5,6] ] ...
- C++ 数组array与vector的比较
转:http://blog.csdn.net/yukin_xue/article/details/7391897 1. array 定义的时候必须定义数组的元素个数;而vector 不需要: 且只能包 ...
- vector定义初始化
头文件 #include<vector> using std::vector; vector<T> v1; vector<T> v2(v1); vector< ...
- vector迭代器用法
#include<iostream> #include<vector> using namespace std; int main() { vector<int> ...
随机推荐
- 6.12---前提两个对象的成员必须一致,才能将有数据的对象将数据传给反射获取的对象conver(有数据对象,目标对象)
//// Source code recreated from a .class file by IntelliJ IDEA// (powered by Fernflower decompiler)/ ...
- LN : leetcode 5 Longest Palindromic Substring
lc 5 Longest Palindromic Substring 5 Longest Palindromic Substring Given a string s, find the longes ...
- 微信小程序打卡第五天
2018-02-1823:55:53大年初三 微信小程序已经学了5个夜晚了,没有很努力,只是简单地接触,感觉从今天开始有了突破的进展,很爽! 无意间发现一个很好的教程,也是一个老哥分享的,很给力 ht ...
- django.db.utils.OperationalError: (1050, "Table '表名' already exists)解决方法
django.db.utils.OperationalError: (1050, "Table '表名' already exists)解决方法 找到解决方案,执行: python mana ...
- QQ应用前景与范围文档
QQ软件 前景与范围文档 当前版本: 版本1 作 者: 李飞 完成日期: 2013年11月3日 1. 业务需求 1.1 应用背景 20世纪后期网络的应用和21世纪的飞速发展,网络已经 ...
- Sonar 规则
bug类型: 1.".equals()" should not be used to test the values of "Atomic" classes. ...
- Git——github基本操作
基本概念 上一篇文章写到git共享仓库,但是有个局限性,就是这个仓库存在于本地,其他人无法从我们这个仓库拿到共享的内容 但是我们可以将这个共享仓库放入一个远程的服务器上,然后设置一些登录权限就能完美的 ...
- jstree的基本使用例子
var menu = (function() { var _menu = {data:{}, initMenu : function() { $.jstree.defaults.core.themes ...
- DeepCloneObjects 和 DeepClone
ARX AcDbDatabase 中的方法 deepCloneObjects() 和 wblock() 区别以及和 AcDbObject 方法 clone() 和 deepClone() 的关系 Ac ...
- JavaScipt30(第六个案例)(主要知识点:给数字加千分号的正则)
承接上文,这是第6个案例: 附上项目链接: https://github.com/wesbos/JavaScript30 这个主要是要实现在给定的json里匹配出搜索框里的city or state, ...