STL容器之Vector
来源:互联网 发布:java b2b2c 开源 编辑:程序博客网 时间:2024/04/27 08:41
1.1 vector 概述
vector的数据安排以及操作方式,与array相似。两者之间的唯一区别在于空间运用的灵活性 vector是动态空间,也就是动态数组 array是静态空间,一旦存储空间确定就不能改变vector的特点: 与array相比,内存的分配更加灵活。 与list相比,在随机存取方面特别高效,但是在插入删除方面显得比较无力。
1.2 vector定义摘要
知其然,更要知其所以然,下面是SGI STL对于vector的定义摘要, 对于只是希望能够使用vector容器的小伙伴们可以直接略过这一节。
在使用vector容器的时候,我们需要包含头文件,但是在SGI STL中对于vector的定义却在
// alloc是SGI STL的空间适配器,说白了就是内存分配管理的类template<class T,class Alloc=alloc>class vector{public://vector的嵌套类型定义 typedef T value_typ; typedef value_type* pointer; typedef value_type* iterator; typedef value_type& reference; typedef size_t size_type; typedef ptrdiff_t difference_type;protected: //下面一行,simple_alloc是SGI STL的空间适配器 typedef simple)alloc<value_type,Alloc> data_allocator; //vector当中的迭代器 iterator start; //指向vector头部的迭代器 iterator finish; //指向vector所存数据的尾部 iterator end_of_storage; //指向所有可用vector尾部 void insert_aux(iterator position, const T& value);//insert函数底层调用的插入函数 void deallocate(){ if(start) data_allocator::deallocator(start, end_of_storage-start); } void fill_initialize(size_type n,const T& value){ start = allocate_end_fill(n,value); finish = start + n; end_of_storage = finish; }public://下面是常用的操作vector当中元素的函数定义 iterator begin(){ return start; } iterator end(){ return finish;} size_type size() const { return size_type(end()-begin());} size_type capacity() const { return size_type(end_of_storage-begin());} bool empty() const { return begin() == end();} reference operator[](size_type n){return *(begin()+n);} //构造函数 vector():start(0),finish(0),end_of_storage(0){} vector(size_type n,const T& value){fill_initialize(n,value);} vector(long n,const T& value){fill_initialize(n,value);} vector(int n,const T& value){fill_initialize(n,value);} explict vector(size_type n){fill_initialize(n,T());}//析构函数 ~vector(){ destory(start,finish);//全局函数 deallocate();//vector的一个member function } reference front(){return *begin();}//获取第一个元素 reference back(){return *(end()-1);}//获取最后一个元素 //vector 尾部插入元素 void push_back(const T& x){ if(finish!=end_of_storage){ construct(finish,x);//全局函数 ++finish; } else insert_aux(end(),x); } //vector尾部弹出元素 void pop_back(){ --finish; destroy(finish); } iterator erase(iterator position){ //删除某个位置上的元素 if(position + 1!=end()) copy(position+1,finish,position);//把后续元素往前移动 --finish; destory(finish); return position; } void resize(type_size new_size, const T& x){ if(new_size<size()) erase(begin()+new_size,end()); else insert(end(),new_size-size(),x); } void resize(size_type new_size){resize(new_size,T());} void clear(){erase(begin(),end());}protected: //配置空间并填满内容 iterator allocate_and_fill(size_type n,const T& x){ iterator result = data_allocator::allocate(n); uninitialized_fill_n(result,n,x);//全局函数 return result; }}//vector的定义当中,如果仅仅是使用vector容器,有些函数可以不去详细了解,但是如果你想了解STL的设计思路和设计结构,这些源码还是很值得细细品味的。
1.3测试vector容器
一个优秀的程序员一定是大量的代码培养起来的,当然我肯定不能算优秀,但是我正在朝着大神的方向努力着,一起来吧,让我们熟练的使用vector,源码是精髓,我们可以去了解,但是我们最重要的目的当然是来使用STL,使用vector容器。
一个vector的测试demo:
#include <iostream>#include <vector>#include <algorithm>using namespace std;int main(){ vector<int> vec;//模板的第二个参数可以省略,这样调用vector()构造函数 cout << vec.capacity() << endl;//查看vector分配的空间大小 cout << "==================" << endl; vec.push_back(3); vec.push_back(8); vec.push_back(10); vector<int>::iterator it; for (it = vec.begin(); it != vec.end(); it++) cout << *it << endl; cout << "==================" << endl; vec.pop_back(); for (it = vec.begin(); it != vec.end(); it++) cout << *it << endl; cout << "====================" << endl; vec.insert(vec.begin() + 1, 15); for (int i = 0; i < vec.size(); i++) cout << vec[i] << endl; cout << "+++++++++++++++++++++++++" << endl; cout << vec.front() << endl; cout<<vec.back()<<endl; cin.get(); return 0;}
总结:
学会了vector的基本使用,尝试着看了,vector的源码,才发现自己所学之知识是多麽的浅显。以上是我对vector动态数组容器的基本认识,为了避免重复造轮子带来时间的浪费,有必要掌握STL,当然如果你是大神,你也可以自己去实现这些功能。点滴的记录,是为了更好的记忆和复习。
1 0
- STL容器之vector
- STL容器之 vector
- STL之vector容器
- STL容器之vector
- STL容器之vector
- STL 之 vector 容器
- STL之vector容器
- STL容器之vector
- STL容器之vector
- STL之vector容器
- STL之vector容器
- STL容器之Vector
- STL容器之vector
- STL容器之vector
- STL容器 之 vector
- STL容器之vector
- 【STL】STL容器之vector
- STL之vector容器(补充)
- 一个C程序是如何生成的
- es6的新特性(一)
- 关于Virtualbox下安装Linux系统
- python学习之 12306的一个小爬虫
- 深入理解javascript(4):__proto__
- STL容器之Vector
- java编程思想(七)总结
- 暴露年龄
- 《Java in Action》-1 第2章 通过行为参数化传递代码
- QT的exe文件移植到其他机器非png图片不显示问题
- linux下日志文件系统
- java网络编程
- Picasso总结
- Classical Sentences from ShanBei Reading