C++ STL introductory tutorial of 3 deque two way queue usage method

  • 2020-05-27 06:42:29
  • OfStack

1. Introduction

deque (Double Ended Queues, two-way queue) is similar to a vector, but it allows for quick insertion and deletion at the head of the container (as in tail 1).

2. Complete program code


/* Be sure to read the following program after you run it */ 
 
#include <deque> 
#include <iostream> 
#include <algorithm> 
#include <stdexcept> 
using namespace std; 
 
void print(int num) 
{ 
  cout << num << " "; 
} 
 
int main() 
{ 
  //1.  Initialize the  
  deque<int> v; 
  deque<int>::iterator iv; 
 
  v.assign(10, 2);// will 10 A value of 2 Is assigned to deque In the  
  cout << v.size() << endl; // return deque The actual number of elements  
  cout << endl; 
 
  //2.  add  
  v.push_front(666); 
  for (int i = 0; i < 10; i++) 
    v.push_back(i); 
  for_each(v.begin(), v.end(), print);// Need to be #include <algorithm> 
  cout << endl; 
  cout << v.size() << endl; 
  cout << endl; 
 
  //3.  Insert and traverse, reverse traverse  
  v.insert(v.begin() + 3, 99); 
  v.insert(v.end() - 3, 99); 
  for_each(v.begin(), v.end(), print); 
  cout << endl; 
  for_each(v.rbegin(), v.rend(), print);// Do it on the reverse iterator ++ The operation will point to the front of the container 1 An element  
  cout << endl; 
 
  //1 The general traversal method  
  for(iv = v.begin(); iv != v.end(); ++iv) 
    cout << *iv << " "; 
  cout << endl; 
  cout << endl; 
 
  //4.  delete  
  v.erase(v.begin() + 3); 
  for_each(v.begin(), v.end(), print); 
  cout << endl; 
  v.insert(v.begin() + 3, 99);// reduction  
 
  v.erase(v.begin(), v.begin() + 3); // Notice it was deleted 3 Elements instead of 4 a  
  for_each(v.begin(), v.end(), print); 
  cout << endl; 
 
  v.pop_front(); 
  v.pop_back(); 
  for_each(v.begin(), v.end(), print); 
  cout << endl; 
  cout << endl; 
 
  //5.  The query  
  cout << v.front() << endl; 
  cout << v.back() << endl; 
 
  // Dangerous practice, however 1 We'll just do it like an array  
  for (int i = 15; i < 25; i++) 
    cout << "Element " << i << " is " << v[i] << endl; 
  // Safe practice  
  int i; 
  try 
  { 
    for (i = 15; i < 25; i++) 
      cout << "Element " << i << " is " << v.at(i) << endl; 
  } 
  catch (out_of_range err)//#include <stdexcept> 
  { 
    cout << "out_of_range at " << i << endl; 
  } 
  cout << endl; 
 
  //6.  empty  
  v.clear(); 
  cout << v.size() << endl;//0 
  for_each(v.begin(), v.end(), print); // already clear . v.begin()==v.end() Nothing will come of it.  
 
  return 0; 
} 

Added 3.

In fact, deque is a combination of the strengths and weaknesses of vector and list. It is in between, an optimized basic sequence container for adding and deleting elements at both ends of a sequence.

It allows relatively fast random access, but unlike vector, which stores all objects in a single contiguous block of memory, it USES multiple contiguous storage blocks and keeps track of those blocks and their order in a mapping structure. The overhead of adding or removing elements to both ends of deque is minimal. It does not require a reallocation of space, so adding elements to the ends is more efficient than vector.

Features:

(1) easy random access, that is, support the [] operator and vector.at (), but the performance is not as good as vector;
(2) it can insert and delete internally, but the performance is not as good as list;
(3) the in-column and out-column operations can be carried out at both ends;
(4) it takes up more memory than verctor.

Reference site: http: / / www cplusplus. com/reference/deque deque /


Related articles: