C + + vector <vector> Learning

Source: Internet
Author: User

In my opinion, this thing about vectors should be an enhanced version of the array.

Need header file when using #include <vector>

Advantages of the vector container: ① is not prone to overflow, it automatically adjusts the data size to accommodate the element you put in ② is that it has many ways to manipulate the elements


Declaration & Initialization:

Vector < type > name

Cases:

Vector <int > A; //Declare an int type vector a

Vector <int > B (10); //declaration of type int vector b with size 10

Vector <int > B (a); //using vector A to initialize vector b

Vector <int > C (10,2); //Declaration size is 10, and the data is initialized to 2 of the vector C

  vector <int > D (C.begin (), C.begin () +2); //using vector C No. 0, 1 element initialization vector D A total of 2 elements

  Of course, you can use an array to initialize the initial, here is not given an example;

Input, Output:

  Like arrays, you can use Cin,cout to perform input and output operations on vectors and, of course, to take advantage of functions

cin>>a[0];

Basic Functions & Operations:

. Size //Returns the number of elements in a vector

. Empty //return vector is null

. Clear //empty elements in a vector

. Erase //delete a (some) element

= //copy vector

= = //Determine if vectors are equal

. Insert //Inserts an element at a location

  . push_back //Inserts an element at the last position of the current vector (can insert a vector for a multidimensional vector)

. Swap //exchange vector

 . Begin (). End () //Returns the starting and the bottom of a vector element

  all of the above operations are vectors (nonsense) ( ̄ε (# ̄) ☆╰╮ ( ̄▽ ̄///)


Two (multi)-dimensional vectors:

In fact, the vector type is used in the declaration type, and there is no other difference between the operation and the one vector, which can be used as a two (multi)-dimensional array

Example: Vector <vector <int > >a

            Ps:↑ here must have a space, no words seem to be because the priority will be error;

  (╯‵-′) ╯︵┻━┻ This white background color how to go AH

Other:

About Size_type:

  

  A type defined by the string class type and the vector class type that holds the length of any string object or vector object, and the standard library type defines size_type as the unsigned type.

------from Wikipedia

Under the thought, is actually because the data storage does not necessarily according to 1 size to allocate, specially has made a to adapt, to save the vector length

Example:vector< vector<int> >::size_type u

For (vector< vector<int> >::size_type u = 0; u < a.size (); u++); //I,j used as a normal count

  

 About iterators:

  Used to traverse the data in a vector

Example: Vector <int >::iterator b

For (B=a.begin (); B!=a.end (); b++);

2016.04.03

C + + vector <vector> Learning

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.