Vector of STL (uncertain length array)

Source: Internet
Author: User

Vector is a collection of the same object. Each object has a corresponding integer index value. Like a String object, the standard library manages class storage related to storage elements. Introduce header files

# Include <vector>

1. Definition and initialization of a vector object
Vector <t> V1 vector stores objects of the T type. Default constructor. V1 is null. Vector <t> V2 (V1) V2 is a copy of V1. Vector <t> V3 (n, I) v3 contains n elements whose values are I. Vector <t> V4 (n) V4 contains N copies of the elements whose values are initialized.

If no element initialization formula is specified, the standard library will provide an initial element value for value initialization. The initial value generated by the library will be used to initialize each element in the container. The specific value depends on the Data Type of the elements stored in the vector.

If the vector stores built-in types such as int, the standard library creates the element initialization type with 0 values.

Vector <int> VV (10); // 10 elements, each initialized to 0


If the vector stores elements of the class type containing the constructor, the standard library uses the default constructor of this type to create the initialization type of the element.

Vector <string> vvvv (10) // 10 elements, each of which is initialized as a Null String

 

In the third case, the element type may be the class type that does not define the constructor. In this case, the standard library still generates an object with an initial value, and each member of this object initializes the value.


In addition, if

Vector <int> VV {10}; // the first element of initialization is 10, and the rest are initialized by default (0 ).

2. Vector object operations

Several important operations

V. empty () If V is null, true is returned; otherwise, falsev is returned. size () returns the number of elements in v. push_back () add an element V [N] whose value is t at the end of V to return the element V1 = v2 whose position is N in V to replace the element V1 = v2 If V1 and V2 are equal, returns true! ==<,<=, >,>= Keep the meanings of these operators

Size () returns the size_type value defined by the corresponding Vector class.

Vector: size_type // Error

Vector <int>: size_type // OK

The push_back () function adds the new element to the end of the vector.

Vector of STL (uncertain length 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.