Introduction to vector type in C ++

Source: Internet
Author: User

Standard Library vector type

Use the required header file:

# Include

Vector: vector is a class template. It is not a data type. Vector is a data type.

 

I. Definition and initialization

Vector V1; // The default constructor V1 is empty.

Vector V2 (V1); // V2 is a copy of V1.

Vector V3 (n, I); // V3 contains n elements whose values are I.

Vector V4 (n); // V4 contains n elements whose values are 0.

Ii. Value Initialization

1> if no element initialization formula is specified, the standard library provides an initialization value for value initialization.

2> If the saved expression contains elements of the class type of the constructor, the standard library uses this type of constructor for initialization.

3> If the saved expression does not have the class elements of the constructor, the Standard Library generates an object with an initial value and uses this object for value initialization.

Iii. Most important operations of vector objects

1. V. push_back (t) add t data at the end of the array

2. V. Size () current data size

3. V. Empty () determines whether the vector is empty.

4. V [N] returns the element whose position is N in v.

5. V1 = V2: Replace the V1 element with a copy of the V2 element.

6. V1 = v2 determine whether V1 and V2 are equal

7 .! =, <, <=,>,> = Maintain the meanings of these operators

Iv. SimplicityProgramExample: Read a set of data to a vector object and calculate and output the sum of adjacent elements of each pair. If the number of read elements is odd, the system prompts that the last element has no sum and outputs its value.

# Include
# Include
Using namespace STD;
Int main ()
{
Vector ivec;
Int ival;

// Read data to a vector object
Cout <"Enter numbers (CTRL + Z)" <while (CIN> ival)
Ivec. push_back (ival );

// Calculate the sum and output of Adjacent Elements
If (ivec. Size () = 0 ){
Cout <"No element ?! "<Return-1;
}
Cout <"sum of each pair of Adjacent Elements in the vector:" <for (vector: size_type IX = 0; ix cout <if (IX + 1) % 6 = 0)
Cout <}

If (ivec. Size () % 2! = 0)
Cout <

Return 0;
}

ArticleSource: DIY tribe (http://www.diybl.com/course/3_program/c++/cppsl/20090212/155317.html)

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.