LeetCode: C++ syntax

A Vector is a sequential container that encapsulates a dynamically sized array and can hold various types.

  • Sort in strict linear order
  • Uses a memory allocator object to handle its storage needs dynamically.

1. Constructor

  • Vector (): Creates an empty vector
  • Vector (int nSize): Creates a vector with nSize elements
  • Vector (int nSize,const t& t): Creates a vector with nSize elements and values t
  • Vector (const vector&): copy constructor
  • Vector (begin,end): Copies the elements of another array within the range [begin,end] into the vector

2. Add functions

  • Void push_back(const T& x): adds an element x to the end of the vector
  • Iterator insert(iterator it,const T& x): A vector in which an iterator refers to an element x
  • Iterator insert(iterator it,int n,const T& x): Increments of n identical elements x before the iterator refers to the element
  • Iterator insert(iterator it,const_iterator first,const_iterator last): A vector in which the iterator refers to the data between [first,last) before inserting another vector of the same type

3. Delete functions

  • Iterator erase(iterator it): Removes the iterator from a vector
  • Iterator erase(iterator first,iterator last): Deletes elements from the vector first,last

Void pop_back(): removes the last element in the vector. Void clear(): clears all elements in the vector. Traversal functions

  • Reference at(int pos): Returns a reference to the pos position element
  • Reference front(): Returns a reference to the first element
  • Reference back(): Returns a reference to the last element
  • Iterator begin(): Returns a vector header pointer to the first element
  • Iterator end(): Returns a pointer to the tail of the vector, pointing to the position next to the last element of the vector
  • Reverse_iterator rbegin(): Reverse iterator referring to the last element
  • Reverse_iterator rend(): Reverse_iterator before the first element

5. Judge the function

Bool empty() const: Checks whether the vector is empty. If it is empty, there are no elements in the vector

6. Size function

  • Int size() const: Returns the number of elements in the vector
  • Int capacity() const: Returns the maximum number of elements that the current vector can hold
  • Int max_size() const: Returns the maximum allowed number of vector elements

7. Other functions

  • Void swap(vector&): Swaps two vectors of the same type
  • Void assign(int n,const T& x): sets the first n elements of the vector to x
  • Void assign(const_iterator first,const_iterator last): Sets elements from [first,last) to the current vector element

1. Push_back Adds data to the end of the array

2. Pop_back removes the last data in the array

3. At gets the data of the numbered position

4. Begin Gets the pointer to the array header

5. End gets a pointer to the last element of the array +1

6. Front gets a reference to the array header

7. Back gets a reference to the last element of the array

8. Max_size gets the maximum size of a vector

9. Capacity Specifies the size allocated by the current vector

10. Size Specifies the size of the current data

11. Resize Changes the size of the currently used data. If it is larger than the currently used data, fill in the default value

12. Reserve changes the size of the space allocated by the current Vecotr

13. Erase Deletes the data item pointed to by the pointer

14. Clear Clears the current vector

15. Rbegin returns the inverted start pointer to the vector (end-1).

16. Rend returns the end pointer to the vector inversion (begin-1)

17. Empty Check whether the vector is empty

18. Swap exchanges data with another vector

  • Vector< type > identifier
  • Vector< type > identifier (maximum size)
  • Vector< type > identifier (maximum size, initial all values)
  • Int I [5] = {1, 2, 3, 4, 5}
  • The Vector < type > vi (I, I + 2); // get the value of I after 3
  • Vector< vector< int> >v; Two-dimensional vector // the outermost <> should have Spaces here. Otherwise it won’t pass under older compilers

Such as the vector < Trie * > children;

class

Class constructor

  • Executed each time a new object of the class is created.
  • The constructor name is exactly the same as the name of the class, and it does not return any type, nor does it return void. Constructors can be used to set initial values for certain member variables
  • It can also take parameters. This assigns an initial value to the object when it is created

Line::Line(double len): length(len) {} Same as :Line ::Line(double len) {}

Class destructor

  1. The destructor of a class is a special member function of a class that is executed each time the object created is deleted.
  2. The name of the destructor is exactly the same as the name of the class, prefixed with a tilde (~), and it does not return any value, nor can it take any arguments. Destructors help free resources before exiting the program, such as closing files, freeing memory, and so on.
class Line { public: void setLength( double len ); double getLength( void ); Line(); // This is the constructor declaration ~Line(); // This is the destructor declaration private: double length; };Copy the code