Vector is part of the C + + Standard Template Library, a versatile template class and function library that can manipulate a variety of data structures and algorithms. Vector is considered a container because it can hold various types of objects like a container, simply put, vector is a dynamic array of any type that can be stored to add and compress data.
In order to be able to use vectors, you must include the following code in your header file:
#include <vector>
Vectors belong to the STD-named domain, so you need to have a naming qualification to complete your code as follows:
Using Std::vector;
Vector<int> vints;
Or even together, using the full name:
Std::vector<int> vints;
It is recommended that you use a global named Domain method: using namespace std;
Function
Expression
C.assign (beg,end) c.assign (N,elem)
Assign the data in the [Beg end] interval to C. Assign a copy of n elem to C.
c.at (IDX)
Returns the data that the index IDX refers to, and throws the Out_of_range if the IDX crosses over.
C.back ()
Returns the last data and does not check if the data exists.
C.begin ()
Returns the first data address in the iterator.
C.capacity ()
Returns the number of data in a container.
C.clear ()
Removes all data from the container.
C.empty ()
Determines whether the container is empty.
C.end ()
Point to the next end element in the iterator, pointing to a nonexistent element.
C.erase (POS)
C.erase (Beg,end)
Deletes the data at the POS location and returns the location of the next data.
Deletes the data from the [beg,end) interval and returns 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)
Inserts a elem copy at the POS location and returns the new data location. Inserts n elem data at the POS position. no return value. Inserts the data in the [Beg,end] interval at the POS position. no return value.
C.max_size ()
Returns the maximum number of data in the container.
C.pop_back ()
Deletes the last data.
C.push_back (Elem)
Add a data to the tail.
C.rbegin ()
Returns the first data of a reverse queue.
C.rend ()
Returns the next position of the last data in a reverse queue.
C.resize (num)
Re-specify the length of the queue.
C.reserve ()
Keep the appropriate capacity.
C.size ()
Returns the number of actual data in the container.
C1.swap (C2)
Swap (C1,C2)
Swap the C1 and C2 elements. Ditto operation.
Vector<elem>
Cvector<elem> C1 (C2)
Vector <Elem> C (n)
Ector <Elem> C (n, Elem)
Vector <Elem> C (beg,end)
c.~ Vector <Elem> ()
Creates an empty vector. Copy a vector. Creates a vector that contains n data, and the data is generated by default constructs. Creates a vector that contains n elem copies. Creates a vector in the [beg;end] interval. Destroys all data and frees up memory.
Operator[]
Returns a reference to the specified position in the container.
Create a vector
Vector containers provide a variety of ways to create, and here are a few common.
To create an empty vector object of the widget type:
Vector<widget> vwidgets;
Create a vector that contains 500 widget type data:
Vector<widget> Vwidgets (500);
Creates a vector that contains 500 widget type data and is initialized to 0:
Vector<widget> vwidgets, Widget (0));
Create a copy of the widget:
Vector<widget> Vwidgetsfromanother (vwidgets);
Add a data to the vector
The vector's default method for adding data is push_back (). The Push_back () function represents adding data to the tail of the vector and allocating memory as needed. For example, to add 10 data to the vector<widget>, you need to write the following code:
for (int i= 0;i<10; i++) {
Vwidgets.push_back (Widget (i));
}
Get the data in the vector where the location is drawn
The data in the vector is dynamically allocated, and a series of allocated spaces using push_back () is often determined by the file or some data source. If you want to know how much data the vector holds, you can use empty (). Gets the size of the vector, you can use size (). For example, if you want to get the size of a vector v, but you don't know if it's empty, or if you've already included the data, if you set it to 1, you can use the following code to implement:
int nsize = V.empty ()? -1:static_cast<int> (V.size ());
Accessing data in a vector
Use two methods to access the vector.
1, Vector::at ()
2, vector::operator[]
Operator[] is primarily intended to be compatible with the C language. It can operate like an array of C languages. But at () is our preferred option because at () has a boundary check and if the access exceeds the vector range, an exception will be thrown. Because operator[] is prone to making some errors, all of us rarely use it, and the following is validated:
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
Vectors can be very easy to add data, but also easy to extract data, the same vector provides erase (), Pop_back (), clear () to delete data, when deleting data, should know to delete the tail of the data, or delete all data, or individual data.
REMOVE_IF () algorithm if you want to use REMOVE_IF (), you need to include the following code in the header file::
#include <algorithm>
Remove_if () has three parameters:
1, iterator _first: An iterative pointer to the first data.
2, iterator _last: An iterative pointer to the last data.
3. Predicate _pred: A conditional function that can operate on an iteration.
Conditional function
A conditional function is a result that returns Yes or no according to a user-defined condition, is the most basic function pointer, or a function object. This function object needs to support all function invocation operations, overloading the Operator () () operation. Remove_if () is inherited through unary_function, allowing data to be passed as a condition.
For example, if you want to delete a matching data from a vector<cstring>, if the string contains a value, starting from this value, end from this value. First, you should create a data structure to contain these, similar to the following code:
#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 conditions are judged:
Class Findmatchingstring:public std::unary_function<cstring, bool> {
Public
findmatchingstring (const LPFINDSTR LPFS):
M_LPFS (LPFS) {
}
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.