C + + system learning two: vectors

Source: Internet
Author: User

Standard library type vector

Definition: A vector represents a collection of objects in which all objects are of the same type.

Access method: Index

Header file:<vector>

Nature: class Template

Note:

    • The template itself is not a class or function, instead it can be thought of as a description written by the compiler to generate a class or function.
    • A vector is a template rather than a type, and the type generated by the vector must contain the type of elements in the vector, such as vector<int>
    • Vectors can accommodate only entity objects and cannot accommodate references
1 defining and initializing vector objects

Also allows you to copy the elements of a vector object to another vector object, but the two vector objects must be of the same type.

List of initialized vector objects

C + + provides several different ways of initializing, which in most cases can be used in a mutually equivalent manner, but there are three special cases:

    • When using copy initialization, only one initial value can be supplied
    • If you provide an initial value within a class, the intelligence is initialized using copy initialization or using curly braces.
    • If you provide a list of initial element values, only the initial values are placed in curly braces for the list initialization, not in parentheses
vector<string> V1 ("a","an","  Name");    // error vector<string> v2{"a"," an ","name"};    // Initialization of lists
Creates a specified number of elements
vector<int> V (ten,-1);    // 10-1vector<string> S (x,"hi");    // hi of 10 strings
Value class

Typically, you can provide only the number of elements that a vector object accommodates without the initial values, and the library provides an initialization value for each element that is determined by the type of object in the vector.

vector<int> V (ten);    // 10 elements, each initialized to a 0vector<string> S (ten);    // 10 elements are initialized to a null character

Note:

When initialized, the parentheses are used to construct the vector object, and if the curly braces indicate a list initialization, other initializations will be considered if the list initialization cannot be performed.

vector<int> v1 (Ten);//V1 has 10 elements, each with a value of 0vector<int> v2{Ten};//The v2 has 1 elements, and the value of the element is tenVector<int> V3 (Ten,1);//V3 has 10 elements, each element has a value of 1vector<int> v4{Ten,1};//The V4 has 2 elements, the values are 10 and 1, respectively.

vector<string> V6 ("HI"); Error, cannot use string literals widget vector object
Vector<string> v7{10}; The list initialization cannot be performed at this time, so it is directly initialized, V7 has 10 default initialized elements
2. Adding elements to the Vector object

Using the member function push_back ();

3. Other vector operation

Note:

Using subscript to access elements, it is necessary to ensure the legitimacy of the subscript, only to the existing elements to perform subscript operations, to ensure that the subscript is valid for the use of the scope for statement.

    vector<int123456789  ten  };      for (Auto &i:v)    {        << i << endl;    }
Iterators

In addition to using subscripts to access the characters of a string object and the elements of a vector object, you can use an iterator to achieve the same effect.

    • Iterators are available for all standard containers, but only a few of them support subscript operators at the same time
Using iterators

Iterators, like pointers, provide indirect access to objects, but there are differences. The get iterator is not a fetch address character, and a type with an iterator has a member that returns an iterator at the same time.

    • If the container is empty, begin and end return the same iterator, which is the trailing iterator
    • Use Auto to define the type of iterators returned by the Receive iterator function
Auto B=v.begin (), E=v.end ();
Iterator operators

The decision principle for the equality of iterators: Two iterators point to the same element or are both end iterators of the same container, or equal.

Iterator type

Iterator and Const_iterator to represent the type of the iterator. Const_iterator can read but cannot modify the value of the element it refers to. If a vector object or a string object is a constant, you can use only const_terator, and if the vector object or string object is not constant, you can use both iterator and const_iterator.

Begin and End operators

The specific types returned by begin and end are determined by whether the object is constant, and if the object is constant, begin and end return Const_iterator, if the object is not constant, returns iterator.

vector<int> v;cosnt vector<int> cv;auto it1=v.begin ();    // the type of it1 is Vector<int>::iteratorauto It2=cv.begin ();    // the type of it2 is Vector<int>::const_iterator

The new C + + standard introduces two new functions, Cbegin and cend, regardless of whether the object itself is a constant, and the return value is const_iterator.

Combining dereference and member access operations
(*it). Menit->men    // The same effect
Some manipulation of the vector object invalidates the iterator
    • Cannot add elements to a vector object in a range for loop
    • Any action that could change the capacity of a vector object will invalidate the iterator of the vector object
Iterator operations

C + + system learning two: vectors

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.