Standard library type vector[c++ Primer]

Source: Internet
Author: User

Vector, which represents the container for the object, and all object types are the same.

Definition and initialization:

Vector<int> A; Default initialization, empty vector, no element

Vector<int> B (a); Equivalent to a copy of all elements in a in b=a;b

Vector<int> C (N, Val); Contains n repeating elements with a value of Val

Vector<int> c (n); Contains n elements, value is initialized by default

Vector<int> d{a,b,c,d..} The element that contains the number of initial values, each of which is given a corresponding value, equivalent to D={a,b,c,d ...}

Vector<string> v7{10}//There are 10 default initialized elements

If parentheses are used, the provided value is used to construct the vector object;

If you use curly braces, you want the list to be initialized

Add an element to a vector object: Take advantage of its member function push_back ()

1:

Vector<int> v;

for (int i=0; i!=100; ++i)

V.push_back (i);

2:

Vector<int> v;

int A;

while (Cin>>a)

V.push_back (a);

Access:

1: Range for statement

Vector<int> v{1,2,3,4,5};

for (auto &i:v)

I+=i;

2: Subscript operator

Vector<unsigned> scores (11,0);

unsigned grade;

while (Cin>>grade)

if (grade<=100)

++SCORES[GRADE/10];

Note: You cannot add an element with the subscript operator.

Standard library type vector[c++ Primer]

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.