zoukankan      html  css  js  c++  java
  • std::vector

    Vector

    Vectors are sequence containers representing arrays that can change in size.

    Just like arrays, vectors use contiguous storage locations for their elements, which means that their elements can also be accessed using offsets on regular pointers to its elements, and just as efficiently as in arrays. But unlike arrays, their size can change dynamically, with their storage being handled automatically by the container.

    vector自动增长的原理:
    Internally, vectors use a dynamically allocated array to store their elements. This array may need to be reallocated in order to grow in size when new elements are inserted, which implies allocating a new array and moving all elements to it. This is a relatively expensive task in terms of processing time, and thus, vectors do not reallocate each time an element is added to the container.

    Instead, vector containers may allocate some extra storage to accommodate for possible growth, and thus the container may have an actual capacity greater than the storage strictly needed to contain its elements (i.e., its size). Libraries can implement different strategies for growth to balance between memory usage and reallocations, but in any case, reallocations should only happen at logarithmically growing intervals of size so that the insertion of individual elements at the end of the vector can be provided with amortized constant time complexity (see push_back).

    Therefore, compared to arrays, vectors consume more memory in exchange for the ability to manage storage and grow dynamically in an efficient way.

    Compared to the other dynamic sequence containers (dequeslists and forward_lists), vectors are very efficient accessing its elements (just like arrays) and relatively efficient adding or removing elements from its end. For operations that involve inserting or removing elements at positions other than the end, they perform worse than the others, and have less consistent iterators and references than lists and forward_lists.

    template < class T, class Alloc = allocator<T> > class vector; // generic template

    Container properties

    Sequence

      Elements in sequence containers are ordered in a strict linear sequence. Individual elements are accessed by their position in this sequence.

    Dynamic array

      Allowsdirect access to any element in the sequence, even through pointer arithmetics, and provides relatively fast addition/removal of elements at the end of the sequence.

    Allocator-aware

      The container uses an allocator object to dynamically handle its storage needs.

    private:
      typedef _Vector_base<_Tp, _Alloc> _Base;
    public:
      typedef _Tp value_type;
      typedef value_type* pointer;
      typedef const value_type* const_pointer;
      typedef value_type* iterator;
      typedef const value_type* const_iterator;
      typedef value_type& reference;
      typedef const value_type& const_reference;
      typedef size_t size_type;
      typedef ptrdiff_t difference_type;
     
    从头文件中可以看出,iterator实质就是一个指针。
     
    vectors use contiguous storage locations for their elements, which means that their elements can also be accessed using offsets on regular pointers to its elements, and just as efficiently as in arrays.
     
    vector<int> ivenc;
    for (vector<int>::iterator iter = ivenc.begin(); iter != ivenc.end(); ++iter)
    {
        *iter = 0;
    }

    续:对vector的操作

  • 相关阅读:
    getline函数
    Java获取某年某月的第一天
    计划任务中使用NT AUTHORITYSYSTEM用户和普通管理员用户有什么差别
    Windows 7系统安装MySQL5.5.21图解
    C#高性能大容量SOCKET并发(十一):编写上传client
    Linux 终端訪问 FTP 及 上传下载 文件
    完毕port(CompletionPort)具体解释
    ping不通的几种可能原因
    Apple Swfit UI控件实现
    js中取session的值
  • 原文地址:https://www.cnblogs.com/black-mamba/p/5951247.html
Copyright © 2011-2022 走看看