尝试下翻译STL里面的一些容易和算法。四级过了,六级刚考。顺便练练自己的英语水平,翻译的不好的地方请大神多多指教哈,方便我改正。
原来均来自:http://www.cplusplus.com/
template < class T, class Alloc = allocator<T> > class vector; // generic template
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.
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 (deques, lists 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.
容器:Vector
原型:
template < class T, class Alloc = allocator<T> > class vector;
描述:vector是一种顺序容器,其行为类似于大小可以改变的array数组。
跟array一样,vector使用连续的存储单元来存储里面的元素。这意味着vector可以使用正常的指针的偏移量来访问其元素。它跟array一样的高效,但是不同于array的是,vector的大小可以动态地改变,存储他们元素的空间可以自动地进行伸缩。
从内部来讲,vector动态地分派空间已存储array里面的元素,当新的元素插入时,这个array需要重新分配空间大小以容纳这个新的元素,这意味着要重新创建一个新的array并且把原来array里面的所有元素都移动到新的array里面。这将花费昂贵的时间代价来完成这项任务,所以,vector并不是每加入一个新的元素就重新分配一次空间。
作为一种解决方案,vector会在一开始就分配比所需空间更多的空间以适应可能的增长,因此,vector会拥有大于所需空间的实际空间(例如,vector实际上只需要存储2个元素,但是会分配5个元素的实际空间)。函数库可以使用不同的策略在内存使用以及重新分配之间达到一个平衡。但不论如何,重分配应该完成在对数时间内,这样,在vector尾部插入单个的元素所需的时间为摊还常量时间。
因此,相比较array,vector消耗更多的内存作为交换以实现高效的动态增长。
相比于其他的动态顺序容器(deques,lists,forward_lists),vector可以非常高效地访问其元素(像array一样高效)以及在尾部增加和删除元素也是相当高效。但是当在尾部以外的位置插入和删除时,它的表现不如其他如list或者是forward_lists容器,并且这会令迭代器以及引用失效。
容器内容:
序列:
在顺序容器里面的元素都严格地排列在一个线性的序列里面。单个元素可以通过他们在序列中的位置直接访问。
动态数组
允许直接访问数组里面的任一元素,甚至是通过指针运算来访问,提供快速在序列尾部添加/删除元素的能力
分配器
容器使用分配器对象来动态地操控其存储所需。
模板参数:
T:
数组vector里面存放的元素类型,
只有T保证不会在移动时抛出异常,当重分配时实现会优化,使用移动元素来代替复制元素(C++11中的移动语义)。
Alloc:
内存分配器对象的类型,一般采用默认值。
//今天就先翻译着一点先。