Usage of C++ deque

API function of deque

Constructor and destructor:
Insert picture description here
non-volatile operations:
Insert picture description here

Variable operations:
Insert picture description here
Insert picture description here

note:

  1. deque does not provide capacity operations: capacity() and reverse().
  2. The insertion and deletion of elements may cause memory reallocation. Therefore, any insert or delete operation will invalidate all pointers, references, and iterators that point to the deque element. The only exception is that after inserting elements at the beginning and end, pointers and references may still be valid.

Commonly used deque API

deque constructor

deque <T> deqT;//The 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

assign(beg, end);//Assign a copy of the data in the interval [beg, end) to itself.
assign(n, elem);//Assign n copies of elem to itself.
deque& operator=(const deque &deq); //overload the equal sign operator
swap(deq);// swap deq with its own elements

deque size operation

deque.size();//Return the number of elements in the container
deque.empty();//Determine whether the container is empty
deque.resize(num);//Re-specify the length of the container to num, if the container becomes longer, Then 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); //Re-specify the length of the container as num. If the container becomes longer, the new position is filled with the elem value. If the container becomes shorter, the elements at the end that exceed the length of the container are deleted.

deque double-ended insert and delete operations

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 of the container
pop_front();//Delete the first data of the container

deque data access

at(idx);//returns the data pointed to by index idx, if idx is out of range, throw out_of_range.
operator[];//Returns the data pointed to by the index idx. If idx is out of range, no exception will be thrown and an error will occur directly.
front();//Returns the first data.
back();//Return to the last data

deque insert operation

insert(pos,elem);//Insert a copy of the elem element at the pos position and return the position of the new data.
insert(pos,n,elem);//Insert n elem data at position pos, no return value.
insert(pos,beg,end);//Insert the data in the [beg,end) interval at position pos, no return value.

deque delete operation

clear();//Remove all data in the container
erase(beg,end);//Delete the data in the [beg,end) interval, and return the position of the next data.
erase(pos);//Delete the data at the pos position and return the position of the next data.

You can also sort deque usage: sort(d.begin(), d.end(), compare);

Guess you like

Origin blog.csdn.net/weixin_43743711/article/details/114746326