Get the App
SLTechnology News&Howtos  ›  Development  › 

How to use vector in STL

Shulou Source: shulou.com Published: 2022-06-01 03:01:51 09月23日 Update

Editor to share with you how to use vector in STL, I believe most people do not know much about it, so share this article for your reference, I hope you can learn a lot after reading this article, let's go to know it!

What is 1.vector?

1.vector is a container that represents a variable size array.

2.vector uses continuous storage space to store elements.

3.vector uses dynamic allocation arrays to store its elements.

2.vector create object

The following are the four copy constructors for vector.

Vector v1 vector v2 (10, 8); / create an object with ten 8 v2 vector v3 (+ + v2.begin (),-- v2.end ()); / / copy construction through an iterator, which can add or subtract operation vector v4 (v3); / / copy construction string s ("hello world"); vector v5 (s.begin (), s.end ())

The process of creating an object by vector is the process of calling its constructor and copy construction, and we can also store the strings in s in container v5.

Note that vector cannot replace string because using vector requires manual printing of'\ 0', and vector does not support a series of operations such as + = and compare sizes.

3. Traversing vector

For a better demonstration, let's first define an array and pass data into it.

Vector v; v.push_back (1); v.push_back (2); v.push_back (3); v.push_back (4); v.push_back (5); (1) subscript traversal

The subscript traversal is readable and writable.

For (int I = 0; I < v.size (); iTunes +) {v [I] + = 1bank / can be modified because the returned value is a reference cout

Tags: Iterations functions elements objects arrays space capacity data allocation content pointer maximum bookshelf that is copy container point article type storage Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Tech Info Redmi MariaDB OPPO Reno Linux