C ++ Primer study note _ 12 _ Standard Template Library _ deque dual-end queue container, _ 12 deque
C ++ Primer study note _ 12 _ Standard Template Library _ deque dual-end queue container
The deque dual-end queue container uses a linear table sequence storage structure like a vector container. However, the only difference with vector is that deque uses a Linear Block Storage Structure to store data. The size of each block is generally 512 bytes, which is called a deque block, all deque blocks are managed using a Map block. Each Map data item records the first address of each deque block. In this way, the deque block can be inserted and deleted at the header and tail, without moving other elements (using the push_back () method to insert elements at the end will expand the queue; and push_front () the method inserts an element in the header and the insert () method in the middle. It only overwrites the element value in the original position and does not add new elements ). In general, deque is more advantageous than vector when considering the memory allocation policies and operation performance of container elements. The structure of the dual-end queue container is as follows.
To use deque, You need to declare the header file # include <deque>
1. Create a deque object
There are usually three methods to create a deque object
(1) create a deque object without any elements, such:
Deque <int> d;
Deque <float> dd;
(2) create a deque object with n elements, such:
Deque <int> d (10 );
(3) create a deque object with that element and assign the initial value, for example:
Deque <int> d (10, 8.5 );
2. Insert elements
(1) using the push_back () method to insert elements from the end will continue to expand the queue.
#include <iostream>#include <deque> using namespace std; int main(int argc, char* argv[]){ deque<int> num; num.push_back(1); num.push_back(2); num.push_back(3); cout << num[0] << " " << num[1] <<" " << num[2] << endl; return 0;}
Running result:
1 2 3
(2) Insert an element from the header. No new element is added, and only the original element is overwritten.
#include <iostream>#include <deque> using namespace std; int main(int argc, char* argv[]){ deque<int> num; num.push_back(1); num.push_back(2); num.push_back(3); num.push_front(10); num.push_front(20); cout << num[0] << " " << num[1] <<" " << num[2] << endl; return 0;}
Running result:
20 10 1
(3) Insert an element from the center. No new elements are added, and only the original elements are overwritten.
#include <iostream>#include <deque> using namespace std; int main(int argc, char* argv[]){ deque<int> num; num.push_back(1); num.push_back(2); num.push_back(3); num.insert(num.begin() + 1, 99); cout << num[0] << " " << num[1] <<" " << num[2] << endl; return0;}
Running result:
1 88 2
3. Forward Traversal
(1) traverse by ARRAY
#include <iostream>#include <deque> using namespace std; int main(int argc, char* argv[]){ deque<int> num; num.push_back(1); num.push_back(2); num.push_back(3); for(int i = 0; i < num.size(); i++) cout << num[i] << ""; cout << endl; return 0;}
Running result:
1 2 3
(2) Previous traversal to the iterator
#include <iostream>#include <deque> using namespace std; int main(int argc, char* argv[]){ deque<int> num; num.push_back(1); num.push_back(2); num.push_back(3); for(deque<int>::iterator iter = num.begin(); iter != num.end();iter++) cout << *iter << " "; cout << endl; return 0;}
Running result:
1 2 3
4. Direction Traversal
Reverse iterator is used to reverse traverse the dual-end queue container
#include <iostream>#include <deque> using namespace std; int main(int argc, char* argv[]){ deque<int> num; num.push_back(1); num.push_back(2); num.push_back(3); for(deque<int>::reverse_iterator riter = num.rbegin(); riter !=num.rend(); riter++) cout << *riter << " "; cout << endl; return 0;}
Running result:
3 2 1
5. Delete Elements
You can delete elements from the header, tail, and center of the double-ended queue container, and clear the double-ended queue container. The following are examples of the operation methods for deleting these four elements.
(1) Use the pop_front () method to delete elements from the header
#include <iostream>#include <deque> using namespace std; int main(int argc, char* argv[]){ deque<int> num; num.push_back(1); num.push_back(2); num.push_back(3); num.push_back(4); num.push_back(5); num.pop_front(); num.pop_front(); for(deque<int>::iterator iter = num.begin(); iter != num.end();iter++) cout << *iter << " "; cout<< endl; return 0;}
Running result:
3 4 5
(2) Use the pop_back () method to delete elements from the end
#include <iostream>#include <deque> using namespace std; int main(int argc, char* argv[]){ deque<int> num; num.push_back(1); num.push_back(2); num.push_back(3); num.push_back(4); num.push_back(5); num.pop_back(); num.pop_back(); for(deque<int>::iterator iter = num.begin(); iter != num.end();iter++) cout << *iter << " "; cout << endl; return 0;}
Running result:
1 2 3
(3) Use the erase () method to delete elements from the center. Its parameter is the iterator position.
#include <iostream>#include <deque> using namespace std; int main(int argc, char* argv[]){ deque<int> num; num.push_back(1); num.push_back(2); num.push_back(3); num.push_back(4); num.push_back(5); num.erase(num.begin() + 1); for(deque<int>::iterator iter = num.begin(); iter != num.end();iter++) cout << *iter << " "; cout << endl; return 0;}
Running result:
1 3 4 5
(4) clear the deque object using the clear () method
#include <iostream>#include <deque> using namespace std; int main(int argc, char* argv[]){ deque<int> num; num.push_back(1); num.push_back(2); num.push_back(3); num.push_back(4); num.push_back(5); num.clear(); cout << num.size() << endl; return 0;}
Running result:
0
Refer:
C ++ primer version 4
ACM Program Design
Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.