Document directory
- Confusing size, capacity, resize, reserve
Vector is equivalent to an array. It is generally called a dynamic array or vector container. Its biggest advantage is that it can access data randomly. read data and insert it at the end. The time complexity of deleting the data is constant time O (1 ). however, if it is not at the end, the time complexity for deleting and inserting data is linear time O (n ).
When using vector, reference the header file # include <vector> and use the namespace using namespace STD;
Common Vector Functions
Assume that the following example is provided:
Vector <int> VEC;
VEC. push_back (11 );
VEC. push_back (22 );
Vector <int> C;
Confusing size, capacity, resize, reserve
If you do not need to use the reserve function, the first three functions will be better understood.
Size indicates the number of elements in the current array. Int totalnum = Vec. Size (); // 2 is returned.
Capacity indicates the size of the reserved array. For example, the number of people who are eating in a restaurant is size, and the total number of seats in the restaurant is capacity. Therefore, capacity> = size
However, the size and capcity are the same by default. when push_back or insert is used, 1 is added at the same time, for example, VEC. push_back (33); the size () is changed to 3, and the capacity () is also changed to 3.
However, VEC. Erase (VEC. Begin (); then the size () is changed to 2, while the capacity is still 3.
Resize indicates changing the array size. Add some elements at the end or Delete the existing elements. assume that the array already contains three pieces of data, VEC. resize (5) indicates adding two data entries at the end of the array and the default value is 0, but the number of the first three values remains unchanged. if it is Vec. resize (5, 88 ). the value of the added number is 88. the previous three numbers remain unchanged. however, if it is Vec. resize (2) indicates that the last of the three data items is deleted. only 2.
The use of reserve is a bit complicated.
The reserve translation indicates the amount of space reserved in the memory for the array.
VEC. Reserve (88); // indicates to reserve 88 locations, then Vec. Capacity () is 88. But Vec. Size () is still 2 at this time, indicating the actual number of elements.
In addition, the parameter in reserve is more meaningful than the value of size (). For example, if the current size () is 10, then Vec. reserve (8) does not play any role, capacity () is still 10, if it is Vec. for reserve (11), capacity () is changed to 11.
Another subtle point is that if reserve (50) continues with other operations, capacity will automatically increase by half when the number of elements is greater than 50.
For example, VEC. Reserve (50 );
VEC. Resize (50 );
//In this case, the capacity value is 50, and the size () value is also 50.
VEC. push_back (11 );
// Capacity: The value is 75. It's amazing. It's 50 + 50/2, and the size () value is 51.
// Proceed
VEC. Resize (76 );
// Capacity is 112 at this time, that is, 75 + 75/2. However, it should be noted that if the increase is more than half of 75, capacity will not be added half, but will be followed by size () it's big.
// For example, if the above is not Vec. Resize (76) But Vec. Resize (113), capacity is also 113. If it is Vec. Resize (114), capacity is also 114.
Function Name |
Explanation |
C. Assign (beg, end) C. Assign (n, ELEM) |
Assign data in the [beg; End) interval to C. For example, C. Assign (VEC. Begin (), VEC. End ()); Assign n copies of ELEM to C. For example, C. assgin (5, 88). c has 5 elements with a value of 88. NOTE: If two different assign functions are used at the same time, the last one will overwrite the previous value.. |
C. At (INDEX) C [Index] |
Returns the data indicated by the index. If the index is out of bounds, out_of_range is thrown. For example, if int ret = C. At (3); // if it is out of bounds, an exception is thrown. You can use try to handle the exception. Int ret = at [3]; // in this way, no exception will be thrown, which is an undefined action. An error value may be returned or crashed. |
C. Back () C. Front () |
Back returns the last data. If the array is empty, crash may occur, and front returns the first element of the array. |
C. Begin () C. End () C. rbegin () C. rend () |
Begin points to the iterator at the beginning. For example, vector <int>: iterator it = Vec. Begin (); End points to the end. Vector <int >:: interator its = VEC. End (); Rbegin is equivalent to end. Rend is equivalent to begin |
C. Capacity () C. Size () |
Size indicates the actual number of elements, and capapcity indicates the reserved space. |
C. Clear () C. Empty () |
Clear removes all data from the container Determine whether the container is empty, such as bool isempty = C. Empty (); |
|
|
Push_back (ELEM) C. pop_back () |
Push_back adds an element to the end. Pop_back is used to delete the tail element. |
C. insert (Pos, ELEM) C. insert (Pos, N, ELEM) C. insert (Pos, beg, end) |
Insert an ELEM copy at the POs position and return the new data location. Insert n ELEM data at the POs position. No return value. Data inserted in the [beg, end) range at the POs position. No return value |
C. Erase (POS) C. Erase (beg, end) |
Delete the data at the POs position and return the location of the next data, for example, C. Erase (C. Begin ()); Delete the data in the [beg, end) interval and return the location of the next data, for example, C. Erase (C. Begin (), C. End ()) |
|
|
|
|
Vector <ELEM> C Vector <ELEM> C (VEC) Vector <ELEM> C (n) Vector <ELEM> C (n, ELEM) Vector <ELEM> C (beg, end) |
Create an empty vector. Copy a vector. Create a Vector Containing N pieces of data, which are generated by default. Create a Vector Containing n elem copies. Create a vector in the [beg; End) Interval |
|
|
C. Swap (VEC) Swap (C, VEC) |
Swap C and VEC elements. Same as above. |
|
|
|
|
C. Resize (Num) |
Specify the queue length again |
C. Reserve () |
Reserve proper capacity |
C. Size () |
Returns numerous numbers of containers. |
|
|
|
|