The vector represents an array container that can change size. Just like an array, the vectors of its elements use contiguous storage locations, which means that you can also access pointers on their elements using offsets that often point to elements, and as efficiently as in arrays. But unlike arrays, their size can be dynamically changed, and their storage containers are automatically processed. Inside the vector, the dynamically allocated array vectors are used to store their content. This array may need to be reassigned so that the scale of the expansion of the new element is inserted, which means assigning it a new array and all the elements. This is a relatively expensive task in terms of processing time, so the vectors are not redistributed every time the element is added to the container. Vector,deque,list three containers belong to the same sequence container, the operation is unified.
Types are as follows:
member Type |
definition |
Notes |
Value_type |
The first template parameter (T) |
|
Allocator_type |
The second template parameter (Alloc) |
Defaults to:allocator<value_type> |
Reference |
value_type& |
|
Const_reference |
Const value_type& |
|
Pointer |
Allocator_traits<allocator_type>::p ointer |
For the default allocator:value_type* |
Const_pointer |
Allocator_traits<allocator_type>::const_pointer |
For the default allocator:const value_type* |
Iterator |
A random access iterator to value_type |
Convertible toconst_iterator |
Const_iterator |
A random access iterator to const Value_type |
|
Reverse_iterator |
Reverse_iterator<iterator> |
|
Const_reverse_iterator |
Reverse_iterator<const_iterator> |
|
Difference_type |
A signed integral type, identical to: Iterator_traits<iterator>::d ifference_type |
Usually the same as ptrdiff_t |
Size_type |
An unsigned integral type so can represent any non-negative value of difference_type |
Usually the same as size_t |
The vector operation interface is as follows: (partly from other implementations)
The implementation of the appellate interface:
//code herefrom Tsinghua University Deng Junhui teachertypedefintRank;//RankTemplate <typename t>classVector {//vector template classprotected: Rank _size;int_capacity; t* _elem;//size, capacity, data area voidCopyFrom (TConst* A, rank lo, rank hi);//Copy array interval a[lo, HI) voidexpand ();//capacity expansion when space is low voidShrink ();//reload factor over-hour compression BOOLBubble (rank lo, rank hi);//Scan Swap voidBubblesort (rank lo, rank hi);//Bubble Sorting AlgorithmRank max (rank lo, rank hi);//Select the largest element voidSelectionsort (rank lo, rank hi);//Select sorting Algorithm voidMerge (rank lo, rank mi, rank hi);//Merge Algorithm voidMergeSort (rank lo, rank hi);//Merge Sort AlgorithmRank partition (rank lo, rank hi);//Axis Point Construction algorithm voidQuickSort (rank lo, rank hi);//Fast Sorting algorithm voidHeapsort (rank lo, rank hi);//Heap Sort (later with full heap explanation) Public://constructor FunctionVector (intc = default_capacity,ints =0, T v =0)//capacity is C, size is s, all elements are initially v{_elem =NewT[_capacity = c]; for(_size =0; _size < S; _elem[_size++] = v); }//S<=cVector (TConst* A, Rank N) {CopyFrom (A,0, n); }//Array Overall replicationVector (TConst* A, rank lo, rank hi) {copyFrom (A, lo, HI);}//intervalVector (vector<t>Const& V) {CopyFrom (V._elem,0, v._size); }//Vector Overall ReplicationVector (vector<t>Const& V, rank lo, rank hi) {copyFrom (V._elem, lo, HI);}//interval// Destructors~vector () {Delete[] _elem; }//free up internal space//read-only access interfaceRank size ()Const{return_size; }//scale BOOLEmpty ()Const{return!_size; }//empty sentence intDisordered ()Const;//determines whether the vector is sortedRank Find (TConst& E)Const{returnFind (E,0, _size); }//unordered vector Overall lookupRank Find (TConst& E, rank lo, rank hi)Const;//unordered vector interval lookupRank Search (TConst& E)Const //ordered vector whole search{return(0>= _size)? -1: Search (E,0, _size); } Rank Search (TConst& E, rank lo, rank hi)Const;//ordered vector interval lookup//Writable Access Interfacet&operator[] (Rank R)Const;//overloaded subscript operator, which can be similar to an array as a reference to each elementVector<t> &operator= (vector<t>Const&);//overloaded assignment operator for direct cloning of vectorsT Remove (Rank R);//Delete an element with rank R intRemove (rank lo, rank hi);//Remove the element rank within the range [lo, HI)Rank Insert (rank R, TConst& E);//inserting elementsRank Insert (TConst& E) {returnInsert (_size, E); }//Insert as end element by default voidSort (rank lo, rank hi);//sort [lo, hi] voidSort () {sort (0, _size); }//Overall Sort voidUnsort (rank lo, rank hi);//Scrambling for [lo, hi] voidUnsort () {Unsort (0, _size); }//Overall scrambling intDeduplicate ();//disorderly de-weight intUniquify ();//orderly de-weight//Traverse voidTraverse (void(* ) (t&));//traversal (using function pointers, read-only or local modification)Template <typename vst>voidTraverse (vst&);//traversal (Use function object, can be globally modified)};//Vector#defineDefault_capacity 3//default initial capacity (can be set to larger in real-world applications)
Attached to:Modifiers interface from C + + reference
-
Assign
-
Assign Vector Content (public member function)
-
Push_back
-
ADD element at the end (public member function)
-
Pop_back
-
Delete last element (public member function)
-
Insert
-
Insert Elements (public member function)
-
Erase
-
Erase Elements (public member function)
-
Swap
-
Swap Content (public member function)
-
Clear
-
Clear Content (public member function)
-
Emplace
-
Construct and insert element (public member function)
-
Emplace_back
-
Construct and insert element at the end (public member function)
C + + vector parsing (c + + 11)