Common usage of vector in leetcode

Time:2019-9-12

Vector introduction

My understanding of Vector is the wrapped array provided by C++, i.e.Collection of objectsGenerally speaking, in the process of brushing questions, ordinary arrays can be replaced by vectors. After all, vectors have relatively simple usage and do not need to consider the length problem. Because it’s the basic usage part, we don’t go into the differences between vectors and arrays, and the characteristics of vectors. Let’s go straight to use them.

This Vector usage note is only for the common usage of leetcode brushing title, so the content may be relatively brief, but only part of the content of the Vector.

In this article, begin and end are iterator types

statement

Although all header files are automatically included in leetcode, header files need to be added if you use vectors for your own programming.

#include <vector>
Plus using namespace std;
perhaps
using std::vector;

Create, copy

Vector < int > a; // Create an empty vector
Vector < int > a (n); // Create a vector containing n objects.
                      // At this point, the data has been constructed by default, such as 0 in the current array.
Vector < int > a (n, elem); // Create a vector containing N elem copies

Vector < int > A1 (a2); // copy a vector
Vector < int > A1 (start, end); // copy data within [begin, end)

As for the last usage, there is a small pit. Let’s note that A2 is a vector with four ints.
If you use vector < int > A1 (a2. begin (), a2. begin () +3), then there are actually three objects in a1.
But if you use vector < int > A1 (a2. begin (), a2. end (), there are actually four objects in a1.
This is because a2. end () actually points toNot the last object of the vector, but the next location of the last object.

Generally speaking, leetcode does not need to manually release the vector memory during the brushing process, so I will not write it.

Access data

A.begin () returns a pointer to the first object, commonly known as an iterator.
A.end () returns a pointer to the next location of the last object
A.begin ()+1 returns a pointer to the next object of the first object
A.end () - 1 returns a pointer to the last object

A. rbegin () returns the pointer to the last object, the reverse iterator
A. rend () returns a pointer to the previous position of the first object, a reverse iterator

Iterators I personally don’t think I need to go into them at first, just know that they are pointers to vector objects.
Reverse iterators are easy to confuse. No, remember if you use it.Rend and end are at the ends of vectors, respectively

A. front () returns data for the first object, the same as * A. begin ().
A. back () returns the data of the last object
A.at (i) returns the object data at the location of number i, checking for the existence of the data
A [i] Returns object data at the location of number I

We recommend that you use a.at(i) to return data as much as possible, because you will check whether the data exists or not.

insert

A. push_back(i); // Simplest insert, adding a data directly to the end of the vector
A. insert (pos, elem); // insert an object before the object pointed to by the POS iterator, noting that it is in front of the object
A. insert (pos, n, elem); /// insert n identical objects before the objects pointed to by POS iterator
A. insert (pos, begin, end); // insert objects between [begin, end] before the objects pointed to by the POS iterator

The last three functions return the location of the new data.

delete

A. clear (); // Delete all objects
A. pop_back(); //Delete the last object
A. erase (pos); //Delete the object corresponding to the POS iterator
A.erase (begin, end); // Delete objects between [begin, end]

The last two functions return the deleted dataNextposition

assignment

A [1] = 1; //Order No. 1 object data is 1
A. assign (1, 1); // make a {1}
A. assign (begin, end); // assign data in another vector [begin, end] to a

Note that the results of the first and second lines are totally different. The assign function is somewhat similar to the replication function and is an operation of the whole.

Other Common Functions

A. size () returns the number of elements in the vector
A. empty () returns whether the vector is empty, empty returns 1, not empty returns 0
Swap (a2); // Exchange a1, A2 data
Swap (a1, a2); // Exchange a1, A2 data, ibid.
Swap (a1 [1], A1 [2]); //Exchange the second and third data of A1 
Sort (begin, end); // Sort data within [begin, end)

Note that there is no use of A1 [1]. swap (a1 [2]).

summary

This paper mainly introduces the following contents of Vector:

  • Create, copy
  • data access
  • Insert, copy, delete
  • Common functions

    • size()
    • empty()
    • swap()
    • sort()

Recommended Today

Hadoop MapReduce Spark Configuration Item

Scope of application The configuration items covered in this article are mainly for Hadoop 2.x and Spark 2.x. MapReduce Official documents https://hadoop.apache.org/doc…Lower left corner: mapred-default.xml Examples of configuration items name value description mapreduce.job.reduce.slowstart.completedmaps 0.05 Resource requests for Reduce Task will not be made until the percentage of Map Task completed reaches that value. mapreduce.output.fileoutputformat.compress false […]