vector(c++)

Introduction

    The vector class is called the vector class, and it implements a dynamic array, an array of objects with varying numbers of elements. Like arrays, the vector class also uses 0-based subscripts to indicate the positions of elements; but unlike arrays, when a vector object is created, the number of elements in the array will increase and decrease with the increase and decrease of the number of elements of the vector object. Automatically change.

vector features:

  1. Occupies a continuous memory space;
  2. The internal implementation is to manage a pointer, but when the memory space is not enough, it will reallocate a larger memory space, usually doubling the capacity;

  3. Vector is very convenient for tail operations, and O(n) time complexity is required for head or insertion.


The commonly used functions of the vector class are as follows:

    1. Constructor
vector(): Create an empty vector
Vector(int nSize): Create a vector with nSize elements
Vector(int nSize, const t& t): Create a vector with nSize elements and all For t
vector(const vector&): copy constructor
vector(begin,end): copy the elements of another array in the [begin,end) range to vector
    2. Add function
void push_back(const T& x): add one to the tail of the vector Element X
iterator insert(iterator it, const T& x): add an element before the iterator points to the element in the vector x
iterator insert(iterator it, int n, const T& x): add n identical elements before the iterator points to the element in the vector Element x
iterator insert(iterator it, const_iterator first, const_iterator last): Insert the data between [first, last) of another vector of the same type before the iterator in the vector points to the element
   3. Delete function
iterator erase(iterator it): delete the vector The iterator points to the element in
iterator erase(iterator first, iterator last): delete the element in [first, last) in the
vector void pop_back(): delete the last element in the vector
void clear(): Clears all elements in the vector
  4. Traversal function
reference at(int pos): Returns the reference of the pos position element
reference front(): Returns the reference of the first element
reference back(): Returns the reference of the tail element
iterator begin( ): Returns the vector head pointer, pointing to the first element
iterator end(): Returns the vector tail pointer, pointing to the next position of the last element of the vector
reverse_iterator rbegin(): Reverse iterator, pointing to the last element
reverse_iterator rend(): Reverse iterator, pointing to the position before the first element
  5. Judgment function
bool empty() const: Judging whether the vector is empty, if it is empty, there is no element in the vector
  6. Size function
int size() const: Return the vector The number of elements in the
int capacity() const: Returns the maximum element value that the current vector Zhang Hong can hold
int max_size() const: Returns the maximum allowable vector element number value
  7. Other functions
void swap(vector&): Swap two Data of a vector of the same type
void assign(int n, const T& x): Set the value of the nth element in the vector to x

void assign(const_iterator first, const_iterator last): The elements in [first, last) in the vector are set to the current vector elements


Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324462422&siteId=291194637