Get the App
SLTechnology News&Howtos  ›  Development  › 

How to use the vector of C++

Shulou Source: shulou.com Published: 2022-06-02 13:32:45 10月29日 Update

This article introduces the knowledge of "how to use C++ 's vector". In the operation of practical cases, many people will encounter such a dilemma. Then let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

Create object

In C++, there are 4-6 methods for creating objects in official documents. Bloggers here introduce four of the most commonly used methods. At the same time, bloggers will save some places that are different from the source documents in order to make them easy to use first.

Create directly, that is, just like the class definition object, the official declaration is: explicit vector ();, where explict means that implicit type conversion is not supported.

Example:

Vector v1; / / create an int type sequence table v1 * * vector v2; / / create a char type sequence table v2 * * vector v3; / / create a double type sequence table v3

By passing n valals to define the object, you can have n val as soon as the object is created, which is officially declared as: explicit vector (size_type n, const value_type& val = value_type ();, where value_type is the type we need.)

Example:

Vector v1 (10jue 9); / / create an int type sequence table v1 with 10 9 position vectors v2 (v1.begin (), v1.begin () + 6); / / create an int type sequence table v2 with 6 9 in it

Create objects in the form of iterator intervals, officially declared as: template vector (InputIterator first, InputIterator last).

Example:

Vector v1 (10jue 9); / / create an int type sequence table v1 with 10 9 position vectors v2 (v1.begin (), v1.begin () + 6); / / create an int type sequence table v2 with 6 9 in it

Create objects through ready-made objects of the same type, officially declared as: vector (const vector& x);.

Example:

Vector v1 (10jue 9); / / create an object by the previous method; vector v2 (v1); / / create object v2, whose content is exactly the same as v1. Iterator

After the baptism of string in the previous sections, I believe you all have a good understanding of iterators, so let's introduce the usage directly to the point:

Ordinary iterator interface: begin () and end (), respectively, where begin () is the first element position and end () is the next position of the last element position

Reverse iterator interfaces: rbegin () and rend (), respectively, where rbegin () is the position of the last element and rend () is the position before the first element.

Example:

Vector v1. Pushback (1); v1.push_back (2); v1.push_back (3); v1.push_back (4); / / these four steps roughly mean inserting 1 2 3 4vector::iterator it = v1.begin (); while (it! = v1.end ()) {cout

Tags: Element object iteration location type data example order official content container commonly used interface clocked interval parameter capacity document output valid Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Technology MariaDB MySQL Linux vpn