standard library vector type learning
Vector Object Initialization method
vector < T > v1;
Default constructor v1 is empty
vector< T > V2 (v1);
V2 is a copy of V1, V2 and v1 must be of the same type, that is, when the type in angle brackets is defined.
vector< T > V3 (n, i);
V3 contains n elements of the value I
vector< T > V4 (n);
The N-copy of the element in the V4 containing the initialization of the value, that is, if the type is a built-in type, the standard library creates an element initializer with a value of 0, and if the type is a class type that contains a constructor, the element initializer is created with the default constructor of that type, for example, the string type;
empty stringSo that when we use this constructor to initialize the vector container, we need to
the element T in the container can provide a single initializer constructor
vector< T > V5 (b, E);
Creates a vector container v5 whose elements are copies of the elements within the scope indicated by iterators B and E
type constraints for elements within a containerThe element type must support the object of the assignment operation element type must be able to replicate
operations of Vector objects
V.empty ()
v.size ()
Returns the number of elements in V, and the value returned is similar to string type, which is
Size_typeType rather than integral type
v.max_size ()
Returns the maximum number of elements that can be accommodated in container V, that is, the maximum possible capacity, which is the largest value of the size_t type
v.capacity ()
Returns the amount of memory a container has requested in advance
v.resize (n)
Adjusts the length of the container v so that it can hold n elements. If n < v.size (), delete the extra elements, otherwise, add a new element with the initialization of the value
v.resize (n, t)
Adjusts the size of V so that it can hold n elements. All
New AdditionsThe element values are T
v.reserve (n)
Adjust Container V's
capacityis n (not the number of elements), if the n value is less than the original capacity, the capacity is unchanged, and if the N value is greater than the original capacity, the capacity value becomes n;
v.push_back (t)
In V's
EndAdds an element with a value of T, returns the void type
V.insert (P, t)
The element that the iterator P points to
FrontInserts a new element of the value T, returns
iterator pointing to the newly added element
V.insert (p, N, t)
The element that the iterator P points to
FrontInserts a new element of n that is T, and returns a void type
V.insert (p, B, E)
The element that the iterator P points to
FrontInserts an element in the range marked by the iterator B and E, returning the void type
V[n]
Container vectors can also use subscript to access elements in a container
v.at (n)
That returns the element with the subscript N.
Reference
V.front ()
That returns the first element of the container v.
Reference
V.back ()
That returns the last element of Container v.
Reference
v.erase (P)
Deletes the element that the iterator p points to, and returns the iterator that points to the element behind the deleted element. If the element that the P itself points to is the last element within the container, then the returned iterator points to the next position beyond the end of the container, that is, ending () returns the position the iterator points to
V.erase (b, E)
Deletes all elements within the scope marked by Iterators B and E. Returns an iterator that points to the element behind the deleted element segment
v.clear ()
Deletes all elements in container V, returns void
V.pop_back ()
Deletes the last element in Container V, returning void
V1 = v2
The vector of the container can also be assigned directly by deleting all elements in the V1 and then copying the elements in v2 to V1
V1.swap (v2)
Exchanging elements in V1 with elements in V2
V.assign (b, E)
Reset the elements of V to copy all elements within the scope of the iterator B and E tags into v. B and E must not be iterators that point to the elements in V (because, when performing a assign operation, the
to first delete all the elements that were originally stored in the container, so if B and e are iterators that point to the elements in V, then these iterators will fail, thus making replication work impossible.
v.assign (n, t)
Reset container V to an element that stores n values of T
V1 = = V2
The container vector has overloaded the = = operator, directly comparing the contents of V1 with V2 rather than the memory location
!=, <=, >=
the subscript operation of a vectorThe vector element is positioned from 0 in the vector, we can access the elements in the container by subscript to change the elements that already exist in the container, but you cannot add elements by subscript
with the subscript operation, we can only use elements that are already present in the vector
iterators
For container vectors, you can also use iterators to access the elements of an object in addition to using the subscript method. An iterator is a data type that examines elements within a container and traverses elementsiterator type of containerEach of the container types defines its own iterator type vector< int >::iterator iter;
This statement defines a variable named ITER, whose data type is the type of iterator defined by vector< int >, where theiterator the same meaning as the actual iterator type begin and end operationsEach container defines a pair of functions named Begin and end, which are used toreturn iterator If there are elements in the container,Bybegin ()The iterator returned by the function points to the first element
Vetor < int >::iterator iter = Ivec.begin ();
Wherein, IVEC is a vector< int > container byEnd ()function returns the vector that the iterator points to.next to the end element, rather than the end element, the iterator returned by this function does not point to any actual element in the vector, it only acts as a sentinel, indicating that we have processed all the elements of the vector.V.rbegin ()function returns a reverse-order iterator that points to the last element of Container Vv.rend ()function returns a reverse-order iterator that points to the position of the front of the first element of Container Vself-increment and reference operation of vector iteratorsThe iterator uses the dereference operator (*) to access the element that the iterator points to, that is, the dereference operator returns the element that the iterator is currently pointing to, and we can use the dereference operator to directly manipulate the element that points to it, that is, to change its value The iterator uses the self-increment operator to move the iterator forward to the next element in the container we can use = = or. = operator to compare two iterators if two iterators point toThe same element, then they are equal, otherwise unequalconst_iteratorThe Const_iterator type can only be used to read elements within a container, but cannot change its value Const_iterator object is not the same as a const iterator object, Const_iterator object is an element that can not change the position it points to. But the const iterator object is itself iterator object is constant, can not be changed, so for a const iterator object, it can only point to the initialization of the specified location, can not be changed, that is, iter++ and other similar operations, But it's the element that can change the position he points to.arithmetic operations of iteratorsITER + (-) n is to add or subtract an integer value on the iterator object.such an operation would result in a new iterator, whose position is iter1-iter2 at the position of N elements before or after ITER's position, and the change expression can be used to compute the distance of two iterator objects, which issigned type of Difference_typeThe valuecontainer-defined iterator iterator: Iterator type for this container typeconst_iterator: Read-only iterator type of elementreverse_iterator: An iterator that addresses elements in reverse orderConst_reverse_iterator: The read-only reverse-order iterator of an element NoteVectors are called containers (one of the containers), but the same type ofObjectThe collection, that is, all objects in a container must be of the same type like vector like a class template, when defined, you need to describe what type of object to save, by placing the type at the angle bracket after the class template name to specify the type (vector ivec;) Vector is not a data type, but a class template that can be used to define any number of data types. Each of the vector types specifies the type of element in which it is saved. Therefore, both vectors and vectors are data types when using the Size_type type, you must indicate where the type is defined. i.e. Vector::size_type (correct), Vector::size_type (error) Any operation that alters the vector lengthmay beInvalidates an existing iterator. This may be because vector in the implementation, will be in advance to apply for a memory, assuming that we are applying for the length of the memory of N, then we perform in push_back () or insert operation, if our element number is not more than N, the iterator will not fail, But when we have more than n the number of elements, then we have to apply twice the memory space, that is 2N, if there is not enough contiguous memory space around the original space, then we need to look for a new location in memory to meet the needs of 2N continuous memory space, when we transfer, At this point the iterator will fail, and if the original location space is sufficient to not transfer, then the iterator will not fail. Therefore, any operation that alters the vector length may cause an existing iterator to fail. However, the continuous memory in the general computer will be very large, so generally will not fail. Samefor delete operations, when we delete one or more elements, the elements that follow the deletion element move forward, so if our iterator points to a position that is not invalidated until the element is deleted, it will be invalidated if it is later, and may be a wild pointer or an incorrect element. That is not the-> pointer to the element that the previous iterator points to. For the reverse iterator (reverse_iterator), the structure body the + + operation accesses the previous element, while the-operation accesses the next element, just as opposed to a normal iterator because the stream cannot be traversed backwards, the stream iterator cannot create a reverse iterator. The following code is vector when adding elements , the way vectors change, we can see that every time you add an element, the Push_back function determines whether the pointer points to the end of the vector, and if you point to the end, call the _GROW_TO function to increase the reserve memory capacity by _capacity the code = Max_ Size ()-_CAPACITY/2 < _capacity? 0: _capacity + _capacity/2; We know that generally increase the 50% of the original memory, so the size of the reserved memory is usually greater than the number of elements equal to the containing
void push_back (const value_type& _val) {//Insert element at end if (_inside _std AddressOf (_val)) {//push back a element size_type _idx = _std AddressOf (_val)-This->_myfirs
T
if (this->_mylast = = this->_myend) _reserve (1);
_orphan_range (This->_mylast, this->_mylast);
This->_getal (). construct (This->_mylast, This->_myfirst[_idx]);
++this->_mylast;
else {//push back a non-element if (this->_mylast = = this->_myend)
_reserve (1);
_orphan_range (This->_mylast, this->_mylast);
This->_getal (). construct (This->_mylast, _val);
++this->_mylast; } void _reserve (Size_type _count) {//ensure room for _count new elements, grow exponentially if (_unu Sed_capacity () < _count) {//need more room, try to get it if (max_size ()-size () < _count)
_xlen ();
_reallocate (_grow_to (Size () + _count)); } size_type _grow_to (size_type _count) const {//Grow by 50% or in least to _count Size_type
_capacity = Capacity (); _capacity = Max_size ()-_capacity/2 < _capacity? 0: _capacity + _capacity/2;
Try to grow by 50% if (_capacity < _count) _capacity = _count;
return (_capacity); }