Vector instead of array

Source: Internet
Author: User

Vector instead of array

1. Declare an int vector to replace the one-dimensional array: vector <int> A; (equal to declaring an int array a[], size is not specified, can be dynamically added to the inside of the delete).

2. Replace the two-dimensional array with vectors. In fact, just declare a one-dimensional array vector, and an array name actually represents its first address, so just declare a vector of the address, that is: vector <int *> A. Same as ideal vectors instead of three-dimensional arrays, vector <int**>a; above and so on.

Here is another method of vectors instead of two-dimensional arrays, which are very similar in usage and arrays.

3. How do I get the elements in a vector? Its usage and array:

For example:

Vector <int *> A

int b = 5;

A.push_back (b);//The function is detailed below

cout<<a[0]; Output is 5

4. Usage

1.push_back add a data at the end of the array
2.pop_back remove the last data from the array
3.at get the data of the numbered position
4.begin get pointer to array header
5.end gets the pointer to the last cell +1 of the array
6. Front getting a reference to the array header
7.back get a reference to the last cell of the array
8.max_size get vector max can be how big
9.capacity the size of the current vector allocation
10.size size of current usage data
11.resize changes the current size of the data used, if it is larger than the current use, the default value is populated
12.reserve changes the size of the space allocated by the current VECOTR
13.erase Delete the data item that the pointer points to
14.clear empty the current vector
15.rbegin returns the starting pointer after the vector reversal (actually the original end-1)
16.rend returns the end pointer of the vector reversal (actually the original begin-1)
17.empty determine if the vector is empty
18.swap exchanging data with another vector

Vector instead of array

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.