[C++] Study Notes (6)----deque container

deque container


container: deque
head File: #include<deque>

Basic concept of deque container

Function

  • Double-ended array, which can perform insertion and deletion operations on the head end

The difference between deque and vector:

  • The insertion and deletion efficiency of vector for the head is low, and the larger the data volume, the lower the efficiency
  • Relatively speaking, deque will insert and delete the head faster than vector
  • Vector accesses elements faster than deque, which is related to the internal implementation of the two

The inner working principle of deque:

There is a central controller inside the deque , which maintains the contents of each buffer, and stores real data in the buffer

The central controller maintains the address of each buffer, making deque like a continuous memory space

  • The iterator of the deque container also supports random access

deque constructor

function prototype

  • deque<T> deqT;//Default construction form
  • deque(beg, end);//The constructor copies the elements in the range [beg, end] to itself
  • deque(n, elem);//The constructor copies n elems to itself
  • deque(const deque &deq);//copy constructor

deque assignment operation

function prototype

  • deque& operator=(const deque &deq);// Overload the equals operator
  • assign(beg, end);// Assign the data copy in the [beg, end] range to itself
  • assign(n, elem);// Assign n elem copies to itself

deque size operation

function prototype

  • deque.empty();// Check if the container is empty

  • deque.size();//returns the number of elements in the container

  • deque.resize(num);//Respecify the length of the container as num, if the container becomes longer, fill the new position with the default value

    // If the container becomes shorter, the elements at the end that exceed the length of the container are deleted

  • deque.resize(num, elem);//Respecify the length of the container as num, if the container becomes longer, fill the new position with elem value

    // If the container becomes shorter, the elements at the end that exceed the length of the container are deleted

deque insertion and deletion

function prototype

Insert operation at both ends:

  • push_back(elem);//Add a data at the end of the container

  • push_front(elem);//Insert a data at the head of the container

  • pop_back();//Delete the last data in the container

  • pop_front();//Delete the first data in the container

Specified position operation:

  • insert(pos, elem);//Insert a copy of the elem element at position pos and return the position of the new data
  • insert(pos, n, elem);//Insert n elem data at pos position, no return value
  • insert(pos, beg, end);//Insert the data in [beg, end] range at position pos, no return value
  • clear();// Clear all data in the container
  • erase(beg, end);//Delete the data in the interval [beg, end] and return the position of the next data
  • erase(pos);//Delete the data at position pos and return the position of the next data

deque data access

function prototype

  • at(int idx);//Return the data pointed to by the index idx
  • operator[];//Return the data pointed to by the index idx
  • front();//returns the first data element in the container
  • back();//returns the last data element in the container

deque sorting

Functional description

  • Using algorithms to sort deque containers

algorithm

  • sort(iterator beg, iterator end);//Sort the elements in the range of beg and end (header file #include<algorithm>) The default sorting rules are ascending order from small to large

For containers that support random access iterators, the sort algorithm can be used to sort them directly

Guess you like

Origin blog.csdn.net/weixin_50202509/article/details/121023774