(This essay is mainly to share with you the use of dynamic array vectors in C + + in the competition, and those who are not related to the race are not said. )
I believe we all know that there is a NB STL template Library in C + +. (Here is a film about STL)
Today we're going to pull a tear. A container in this: a dynamic array vector.
The vector is actually similar to a[], which means it overloads the [] operator. (Really 66666666)
You need to add <vector> this header file when using vectors.
Definition method: Vector < type name > variable name;
Cases:
Vector <int><char>//data is a custom datatype that can be a struct
Operation Method:
| A[i] |
Returns the element I in a dynamic array |
| A.empty () |
Returns true if the dynamic array is empty, otherwise false |
| A.size () |
Returns the number of elements in a dynamic array |
| A.resize (x) |
Modify the dynamic array size to X |
| A.push_back (x) |
Inserts x into the dynamic array trailer |
| A.pop_back () |
Delete elements of a dynamic array trailer |
| A.begin () |
Returns a pointer to the head of a dynamic array |
| A.end () |
Returns a pointer to the last element of the trailing element of the dynamic array |
Get a sort of practical combat.
Right on the code.
Lazy writing qsort directly with the template.
#include <iostream>#include<cstdio>#include<vector>#include<algorithm>using namespaceStd;vector<int>A;intMain () {intN; scanf ("%d",&N); for(intI=0; i<n;i++) { inttmp; scanf ("%d",&tmp); A.push_back (TMP); } sort (A.begin (), A.end ()); for(intI=0; i<n;i++) printf ("%d", A[i]); return 0;}
C + + dynamic array vector