Use vector container

Define and initialize the container 1 vector

1) In addition to the container array, other containers (either sequential or associated with the container vessel) defines a default constructor, create an empty container of the type specified.

2) specifies the number of list initialization value and each element of the container element container.

3) The container is initialized to a copy of another container, two types of containers and their data types must match.

4) except that 3) in the container copy format, it may be passed two iterators to copy a range of parameters, copy this embodiment does not require the same type of container or even the same type of elements is not required, as long as the element type of copy can be converted to the type of the initialization container element.

5) the size of the container may be provided in the container initialization sequence is specific manner. Order to provide a container receiving container size and initializer (optional) as constructor arguments, if the initial value of the element is not provided, then the element according to the initialization value of the initialization manner. Built-output type are initialized to 0, its default constructor class type is initialized.

 

2 assignment and swap

 Assign member functions consistent manner such that the assignment to be initialized, the function can assign different but compatible types of assignments.

As the old elements in the container to be replaced, so the assignment before the container iterators can no longer perform the container.

In addition to the array, swap function simply exchanging the internal data structure of the two containers, the element itself does not exchange operation is performed quickly. Although the elements of not moving so iterators, pointers and references did not fail, but these elements have been part of different containers, but for the string, the post-swap calls iterators, pointers, and references will fail.

 

3 vector operations container size

 

4 vector support relational operators

container supports vector as built-in types and relational operators, comprising ==,! =,>,> =, <, <=. And string comparison operators manner similar to:

Is equal to 1) corresponding to the size of the element and equal, then the container is equal; otherwise, unequal container.

2) two containers mutually prefix sequence, depending on the comparison result is not equal to the first element.

 

5 iterator

1) the scope of the iterator

A range indicated by an iterator iterator, two iterators point to the position (one past the last element) of the element after the same vessel and the last element. The two iterators commonly referred begin and end. It may be described mathematically left closed interval [begin, end).

2) iterator type

vector container defines four types of iterators, respectively iterator, const_iterator, reverse_iterator, const_reverse_iterator. There Returns an iterator type of member functions begin () / end (), cbegin () / cend (), rbegin () / rend (), crbegin () / crend ​​(). Wherein compared reverse iterator reverse_iterator / const_reverse_iterator the forward iterator, xbegin () Returns the iterator to the last element, xend () Returns the iterator to the previous element of the first element.

Whether const member functions begin () / rbegin () Returns the like iterator type is a constant depending on the container type whether the constant type. And when the pointer const reference like, may be returned iterator type constant amount from the vessel through a very const member functions cbegin () / crbegin () and the like.

vector container iterator supports operators are ① * iter returns an iterator iter reference to an element referred to; ②iter-> mem referred returns an iterator iter member element; ③ ++ iter / - iter; ④iter1 == iter2 / ! iter1 = iter2; ⑤iter - n / iter + n; ⑥iter - = n / iter + = n; ⑦iter1 - iter2; ⑧> /> = / </ <=.

 

Vector elements are added to the vessel 6

vector container defines two functions for the operation of the additional element, respectively push_back () and insert (), wherein the insert () function with a plurality of heavy duty versions.

1) insert (q, t), prior to insertion into the iterator element q t, iterator returns a pointer to the newly added elements;

2) insert (q, n, t), prior to insertion into the iterator q n th t, returns an iterator to the first element of the newly added;

3) insert (q, b, e), is inserted into the iterator before the iterator q B, the specified range of elements e, B, e can not be performed iteratively iterator element itself, it returns a pointer to the first element of the newly added device;

4) insert (q, il), to the order before the iterator q insert element list element values, the first iterator returns a new element added;

Because push_back () and insert () when you insert the class type to a container, it will create a temporary object is then copied into the container, which caused a waste of space and efficiency, the new standard also introduces emplace () and emplace_back (). They can be passed in accordance with the first parameter type (constructor must respective conventional) memory elements configured to directly manage the space of the container.

 

7 access vector container element

Are the corresponding elements of the operation returns to the above four references, if only access the element, and then use the auto reference types are variable, but if you want to change the variable value will be performed is defined as a reference element, 2) as shown in .

1) front () function returns the first element that contains the reference.

2) () function returns a reference to the element end of the container back.

. 3) [n] is returned to the vessel in the subscript n in application elements.

4) at (n) returns the container subscript n the application, if n is out of range, it will throw an exception out_of_range.

 

8 delete elements in vector

1) pop_back () to delete the last element of the container.

2) erase (q) q remove iterator pointing element, a return element iterator delete elements.

3) erase (b, e) deleting the iterator B, the specified range of elements e, iterator returns a Delete element after the last element.

4) clear () to empty the container.

 

Adjusting the size of the container 9 vector

a resize (n) is the adjusted container size n, if n is less than the current size of the container, remove excess elements, whereas the add a new element and the tail element value is initialized. resize (n, t) and resize (n) as in the container is resized, the new element initialized as t.

 

The blog articles are their own learning blog, limited, if there are places where understanding wrong, I hope you can correct me!

Guess you like

Origin www.cnblogs.com/zpchya/p/11098605.html