C + + dynamic array vector

Source: Internet
Author: User

(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

Contact Us

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.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.