Vector is part of the C ++ standard template library. It is a versatile template class and function library that can operate on multiple data structures and algorithms. Vector is considered a container because it can store various types of objects like a container. Simply put, vector is a dynamic array that can store any type, data can be added and compressed.
To use vector, you must include the following code in your header file:
# Include <vector>
Vector belongs to the STD naming field. Therefore, you need to complete the Code as follows:
Using STD: vector;
Vector <int> vints;
Or, use the full name:
STD: vector <int> vints;
We recommend that you use the global naming domain: Using namespace STD;
Function
Statement
C. Assign (beg, end) C. Assign (n, ELEM)
Assign the value of the data in the [beg; End) interval to C. Assign a copy of n elem values to C.
C. At (idx)
Returns the data indicated by the index idx. If idx is out of bounds, out_of_range is thrown.
C. Back ()
Returns the last data and does not check whether the data exists.
C. Begin ()
Returns the first data address in the iterator.
C. Size ()
Returns the number of data in the container.
C. Clear ()
Remove all data from the container.
C. Empty ()
Determines whether the container is empty.
C. End ()
Point to the next end element in the iterator and to a non-existent element.
C. Erase (POS)
C. Erase (beg, end)
Delete the data at the POs position and return the location of the next data.
Delete the data in the [beg, end) interval and return the location of the next data.
C. Front ()
Returns the first data.
Get_allocator
Returns a copy using the constructor.
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. max_size ()
Returns the maximum number of data in the container.
C. pop_back ()
Delete the last data.
C. push_back (ELEM)
Add a data entry to the end.
C. rbegin ()
Returns the first data of a reverse queue.
C. rend ()
Returns the next location of the last data in a reverse queue.
C. Resize (Num)
Specify the length of the queue again.
C. Reserve ()
Reserve the appropriate capacity.
C. Size ()
Returns the actual number of data in the container.
C1.swap (C2)
Swap (C1, C2)
SWAps C1 and C2 elements. Same as above.
Vector <ELEM>
Cvector <ELEM> C1 (C2)
Vector <ELEM> C (n)
Ector <ELEM> C (n, ELEM)
Vector <ELEM> C (beg, end)
C .~ Vector <ELEM> ()
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. Destroys all data and releases memory.
OPERATOR []
Returns a reference from a specified position in the container.
Create a vector
The vector container provides multiple creation methods. The following describes several common methods.
Create an empty vector object of the widget type:
Vector <widget> vwidgets;
Create a vector containing 500 widget data:
Vector <widget> vwidgets (500 );
Create a vector containing 500 widget data and initialize it to 0:
Vector <widget> vwidgets (500, widget (0 ));
Create a copy of a widget:
Vector <widget> vwidgetsfromanother (vwidgets );
Add a data entry to the vector
The default method for adding data to a vector is push_back (). The push_back () function adds data to the end of the vector and allocates memory as needed. For example, to add 10 pieces of data to vector <widget>, you need to write the following code:
For (INT I = 0; I <10; I ++ ){
Vwidgets. push_back (widget (I ));
}
Obtain the location data in the vector.
Data in a vector is dynamically allocated. A series of space allocated using push_back () is often determined by files or some data sources. If you want to know how much data a vector stores, you can use empty (). Returns the vector size. You can use size (). For example, if you want to obtain the size of a vector V, but you do not know whether it is null, or if it already contains data, you can use the following code:
Int nsize = V. Empty ()? -1: static_cast <int> (V. Size ());
Access data in a vector
Use two methods to access the vector.
1. Vector: ()
2. Vector: operator []
OPERATOR [] is mainly used for compatibility with the C language. It can be operated like a C array. However, at () is our first choice, because at () performs a boundary check. If the access exceeds the vector range, an exception is thrown. Since operator [] may cause some errors, we seldom use it. perform the following verification:
Analyze the following code:
Vector <int> V;
V. Reserve (10 );
For (INT I = 0; I <7; I ++ ){
V. push_back (I );
}
Try {int ival1 = V [7];
// Not bounds checked-will not throw
Int ival2 = V. At (7 );
// Bounds checked-will throw if out of range
} Catch (const exception & E ){
Cout <E. What ();
}
Delete data in a vector
Vector can easily add and retrieve data. Similarly, vector provides erase (), pop_back (), and clear () to delete data. When deleting data, you should know whether to delete the tail data, delete all data, or some data.
To use the remove_if () algorithm, you must include the following code in the header file ::
# Include <algorithm>
Remove_if () has three parameters:
1. iterator _ First: iteration pointer to the first data.
2. iterator _ last: iteration pointer pointing to the last data.
3. Predicate _ Pred: a conditional function that can operate on iterations.
Conditional Functions
A conditional function is a result that returns yes or no according to user-defined conditions. It is the most basic function pointer or a function object. This function object must support all function call operations and overload operator () operations. Remove_if () is inherited by unary_function and data can be transmitted as a condition.
For example, if you want to delete the matched data from a vector <cstring>, if the string contains a value, it starts from this value and ends from this value. First, you should establish a data structure to include the data. The Code is as follows:
# Include <functional>
Enum findmodes {
Fm_invalid = 0,
Fm_is,
Fm_startswith,
Fm_endswith,
Fm_contains
};
Typedef struct tagfindstr {
Uint imode;
Cstring szmatchstr;
} Findstr;
Typedef findstr * lpfindstr;
Then the processing condition is judged:
Class findmatchingstring: public STD: unary_function <cstring, bool> {
Public:
Findmatchingstring (const lpfindstr lpfs ):
M_lpfs (lpfs ){
}
Bool operator () (cstring & szstringtocompare) const {
Bool retval = false;
Switch (m_lpfs-> imode ){
Case fm_is :{
Retval = (szstringtocompare = m_lpfdd-> szmatchstr );
Break;
}
Case fm_startswith :{
Retval = (szstringtocompare. Left (m_lpfdd-> szmatchstr. getlength ())
= M_lpfdd-> szwindowtitle );
Break;
}
Case fm_endswith :{
Retval = (szstringtocompare. Right (m_lpfdd-> szmatchstr. getlength ())
= M_lpfdd-> szmatchstr );
Break;
}
Case fm_contains :{
Retval = (szstringtocompare. Find (m_lpfdd-> szmatchstr )! =-1 );
Break;
}
}
Return retval;
}
PRIVATE:
Lpfindstr m_lpfs;
};
With this operation, you can effectively delete data from the vector:
Findstr FS;
FS. imode = fm_contains;
FS. szmatchstr = szremove;
Vs. Erase (STD: remove_if (vs. Begin (), vs. End (), findmatchingstring (& FS), vs. End ());
All the removal operations, such as remove () and remove_if (), are based on an iteration range and cannot operate the data in the container. So when remove_if () is used, the above data in the container is actually operated.
We can see that remove_if () is actually modifying the iteration address based on the condition. There is some residual data behind the data and the data to be deleted. The location of the remaining data may not be the original data, but they do not know.
Call erase () to delete the residual data. Note that in the preceding example, the remove_if () and vs. ENC () data are deleted through erase.