Home > Article > Backend Development > What is the usage of c++ vector
c The usage of vector is: 1. Create vector object; 2. Insert numbers at the end; 3. Use subscripts to access elements; 4. Use iterators to access elements; 5. Insert elements; 6.) Delete elements, etc. wait.
In c, vector is a very useful container. The usage of c vector is:
1. Basic operations
(1)Header file#include7d10b7d419803d4062679b4587905232.
(2)Create vector object, vectorbd43222e33876353aff11e13a7dc75f6 vec;
( 3) Insert numbers at the end: vec.push_back(a);
(4) Use subscripts to access elements, cout8c7d64dba6eb821d3deff7eae5231daa
reverse(vec.begin(),vec.end());
Flip the elements (in vector, if two iterators are required in a function, usually neither of the latter is included.)
(2), use sort to sort: header file is required #include< ;algorithm>,
sort(vec.begin(),vec.end());
(The default is to sort in ascending order, that is, from small to large).
You can compare in descending order by overriding the sort comparison function, as follows:
Definition Sorting comparison function:
bool Comp(const int &a,const int &b) { return a>b; }
When called: sort(vec.begin(),vec.end(),Comp), this will sort in descending order.
Related learning recommendations: C video tutorial
The above is the detailed content of What is the usage of c++ vector. For more information, please follow other related articles on the PHP Chinese website!