c++序列式容器之vector

0.vector源代码

// alloc 是SGI STL的空间配置器
template<class T,class Alloc=alloc>
class vector{
   public:
     //vector的嵌套型别定义
     typedef  T             value_type;
     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;
     iterator start;//表示目前使用空间的头
     iterator finish;//表示目前使用空间的尾
     iterator end_of_storage;//表示目前可用空间的尾

     void insert_aux(iterator position,const T& x);
     void deallocate(){
         if(start)
            data_allocator::deallocate(start,end_of_storage-start);
     }

     void fill_initialize(size_type n,const T& value)
     {
         start=allocate_and_fill(n,value);
         finish=start+n;
         end_of_storage=finsih;
     }

  public:
     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(int n,const T& value){fill_initialize(n,value);}
     vector(long n,const T& value){fill_initialize(n,value);}
     explicit  vector(size_type n){fill_initialize(n,T());} 

     ~vector(){
         destroy(start,finish);
         deallocate();
     }

     reference front(){return *begin();}//第一个元素
     reference back() {return *(end()-1);}//最后一个元素
     void push_back(const T& x){//将元素插入至最尾端
         if(finish!=end_of_storage){
             construct(finish,x);
             ++finish;
         }
         else
            insert_aux(end(),x);
     }

     void pop_back(){//将最尾端元素取出
         --finish;
         destroy(finish);//全局函数
     }

     iterator erase(iterator position){//清除某位置上的元素
         if(position+1 !=end)
         {
            copy(position+1,finish,position);//后续元素往前移动
         }
         --finish;
         destroy(finish);
         return position;
     }

     void resize(size_type 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;
     }
};

1.定义和初始化vector对象

vector<T> v1;       //v1是一个空vector,执行默认初始化

vector<T> v2(v1) ;  //v2中包含v1所有元素的副本

vector<T> v2 = v1;  //跟上面等价

vector<T> v3(n, val) ; //v3包含了n个val元素

vector<T> v4(n);   //v4中包含n个重复执行了值初始化的对象,值初始化,int型为0,string型为空字符

vector<T> v5{a,b,c...}; //v5中包含了初始值个数的元素,列表初始化 。vector<T> v5(a,b,c..);错误

vector<T> v5 = {a,b,c...}; //同上

vector<int> tmp(vec.begin(), vec.begin() + 3);    //用向量vec的第0个到第2个值初始化tmp

例:

vector<int> v1(10);  //v1有10个元素都为0

vector<int> v2{10};  //v2有一个元素,为10

vector<int> v3(10,1);  //v3有10个元素,都为1

vector<int> v4{10, 1};  //v4有两个元素,为10,1

2.vector的元素操作

(1). 容量

  • 向量大小: vec.size();
  • 向量最大容量: vec.max_size();
  • 更改向量大小: vec.resize();
  • 向量真实大小: vec.capacity();
  • 向量判空: vec.empty();
  • 减少向量大小到满足元素所占存储空间的大小: vec.shrink_to_fit(); 

(2). 修改

  • 多个元素赋值: vec.assign(); //类似于初始化时用数组进行赋值,其旧元素会被替换
  • 末尾添加元素: vec.push_back();
  • 末尾删除元素: vec.pop_back();
  • 在指定位置position插入元素val: vec.insert(const_iterator position, value_type& val);
  • 在指定位置position插入n个元素val:vec.insert(const_iterator position, size_type n, value_type& val);
  • 在指定位置插入同类型容器目标区间内的元素:vec.insert(const_iterator position, InputIterator first, InputIterator last);
  • 删除指定位置元素: vec.erase(const_iterator position);    vec.erase(const_iterator first, const_iterator last);删除指定区间元素
  • 交换两个向量的元素: vec.swap();
  • 清空向量元素: vec.clear();

(3)迭代器

  • 开始指针:vec.begin();
  • 末尾指针:vec.end(); //指向最后一个元素的下一个位置
  • 指向常量的开始指针: vec.cbegin(); //意思就是不能通过这个指针来修改所指的内容,但还是可以通过其他方式修改的,而且指针也是可以移动的。
  • 指向常量的末尾指针: vec.cend();

(4)元素的访问

  • 下标访问: vec[1]; //并不会检查是否越界
  • at方法访问: vec.at(1); //以上两者的区别就是at会检查是否越界,是则抛出out of range异常
  • 访问第一个元素: vec.front();
  • 访问最后一个元素: vec.back();
  • 返回一个指针: int* p = vec.data(); //可行的原因在于vector在内存中就是一个连续存储的数组,所以可以返回一个指针指向这个数组。这是是C++11的特性。

猜你喜欢

转载自www.cnblogs.com/chenguifeng/p/11746447.html