On the C ++ STL vector container
Benpian brief essay \ (C ++ STL \) in \ (vector \) to use containers and common tips. \ (Vector \) containers are \ (C ++ STL \) one more basic container. When we learn this container, the container must not only learn the specific use, but also from the experience \ (C ++ STL \) concept.
The concept of vector container
\ (vector \) is a vector of meaning in English. If the learned high school mathematics compulsory four plane vector or their compulsory high school physics have an intuitive understanding of a first class. But in \ (STL \) in, \ (the Vector \) and physics, geometry and other things has nothing to do.
We know that an array must have a fixed length, in an array of open time, this length will be statically determined. But \ (vector \) is an array of "enhanced version" for a set of data is concerned, you go \ (vector \) in how much data is stored, \ (vector \) length is as big. In other words, we can be understood as a "variable-length arrays."
In fact, \ (Vector \) implementation is based on the multiplication idea : If \ (Vector \) is the actual length of \ (n-\) , \ (m \) of \ (Vector \) of the current maximum length, then the addition of a time element, look at, if the current \ (n-m = \) , then the dynamically apply a \ (2M \) amount of memory. Conversely, when removed, if \ (n-\ GE \ FRAC} {2} {m \) , then the release half the memory.
Disclaimer vector container
\ (vector \) container stored in the template library: #include<vector>
Lane, before using the need to open the library.
\ (vector \) declared the vessel follows the \ (C ++ STL \) of the general statement of principles:
Container type <variable type> Name
Example:
#include<vector>
vector<int> vec;
vector<char> vec;
vector<pair<int,int> > vec;
vector<node> vec;
struct node{...};
The method of using the vector container
\ (Vector \) to use a container substantially as shown in the table:
usage | effect |
---|---|
vec.begin(),vec.end() |
Returns the vector of the first and last iterators |
vec.front(),vec.back() |
Returns the vector of the first and last elements |
vec.push_back() |
Add an element to the end of the vector |
vec.size() |
Returns the current vector length (size) |
vec.pop_back() |
Remove an element from the end of the vector |
vec.empty() |
Returns whether the vector is empty, 1 is empty, 0 is not empty |
vec.clear() |
Empty vector |
In addition to those mentioned above, our \ (vector \) container to support random access, which can be used like an array \ ([\, \,] \) to the value. Remember, not all \ (STL \) containers have this property! In (STL \) \ learning process, be sure to clear the similarities and differences between the various containers!