Reprinted from https://www.cnblogs.com/linuxAndMcu/p/10260124.html
I. Overview
the deque (deque) is constituted by a section of contiguous space quantitative, it can develop to the ends, so both placed at the tail or head elements rapidly. Placement element at the intermediate portion is relatively time-consuming, because the other elements must be moved.
Second, the definition and initialization
Must be added to the container prior to use corresponding header file:
#include <the deque> // the deque belongs to namespace std, defined by naming it is necessary, for example using std :: deque;Defined codes are as follows:
// definition of a double-ended queue int type A STD :: <the deque int > A; // definition of a type of double-ended queue int a, and sets the initial size of 10 STD :: the deque < int > A ( 10 ); // definition of a type of double-ended queue int a, and sets the initial size of 10 and the initial value are. 1 STD :: the deque < int > A ( 10 , . 1 ); // define and initialize by a deque deque B STD :: the deque < int > B (a); // to a deque of from 0 to 2 (of 3) as an initial value of b deque STD :: the deque < int > b (a.begin (), a.begin () + 3 );In addition, you can also use an array to direct an initialization vector:
int n[] = { 1, 2, 3, 4, 5 }; // The first five elements of the array of n as a deque Initial Value @ Description: excluding of course arr [4] the element, the end of the next element pointer refer to the end of the element, // this is mainly to and deque.end () pointer unity. the deque :: STD < int > A (n-, n-+ . 5 ); // The n [1], n [2 ], n [3] as an initial value of a deque STD :: the deque < int > a (n-& [ . 1 ], n-& [ . 4 ]);
Third, the basic operating functions
3.1 capacity function
- Container size: deq.size ()
- Container maximum capacity: deq.max_size ()
- Change the size of the container: deq.resize ()
- Empty containers sentence: deq.empty ()
- Reducing the size of the container to meet the size of the storage space occupied by the elements: deq.shrink_to_fit ()
#include <iostream> #include <deque> int main(int argc, char const* argv[]) { std::deque<int> deq; for (int i = 0; i<6; ++i) { deq.push_back(i); } :: COUT STD << deq.size () :: STD << endl; // Output:. 6 STD :: << deq.max_size COUT () :: STD << endl; // Output: 1073741823 deq.resize ( 0 ); // change the element size std :: cout << deq.size () << std :: endl; // output: 0 IF (deq.empty ()) :: cout std << " element is empty " :: endl << std; // output: element is empty return 0; }3.2 Add Function
- Head additive element: deq.push_front (const T & x)
- Added to the end elements: deq.push_back (const T & x)
- Insert element anywhere: deq.insert (iterator it, const T & x)
- Inserted anywhere n identical elements: deq.insert (iterator it, int n, const T & x)
- Into another vector [forst, last] data between: deq.insert (iterator it, iterator first, iterator last)
#include <iostream> #include <deque> #include <iterator> int main(int argc, char const* argv[]) { std::deque<int> deq; // head element increases deq.push_front ( . 4 ); // added to the end elements deq.push_back ( . 5 ); // arbitrary position of the insertion element STD :: the deque < int > :: = Iterator deq.begin (); deq.insert(it, 2); // arbitrary position of the insertion of n identical elements = deq.begin (); // must have sentence deq.insert (IT, . 3 , . 9 ); // into another vector [forst, last] data between the deque < int > deq2 ( . 5 , . 8 ); IT = deq.begin (); // must have sentence deq.insert (IT, deq2.end () - . 1 , deq2.end ()); // Traversing the display for (IT = deq.begin ();! IT = deq.end (); IT ++ ) std::cout << *it << " "; // 输出:8 9 9 9 2 4 5 std::cout << std::endl; return 0; }3.3 Delete Function
- Head remove elements: deq.pop_front ()
- End remove elements: deq.pop_back ()
- Anywhere delete an element: deq.erase (iterator it)
- Remove elements between the [first, last]: deq.erase (iterator first, iterator last)
- Clear all the elements: deq.clear ()
#include <iostream> #include <deque> #include <iterator> int main(int argc, char* argv[]) { std::deque<int> deq; for (int i = 0; i < 8; i++) deq.push_back(i); // header delete elements deq.pop_front (); // end delete elements deq.pop_back (); // anywhere delete an element STD :: the deque < int > :: = Iterator IT deq.begin (); deq.erase(it); // delete the [first, last] elements between deq.erase (deq.begin (), deq.begin () + . 1 ); // Traversing the display for (IT = deq.begin ();! IT = deq.end (); IT ++ ) std::cout << *it << " "; std::cout << std::endl; // clear all elements deq.clear (); // Traversing the display for (IT = deq.begin ();! IT = deq.end (); IT ++ ) std::cout << *it << " "; // 输出:3 4 5 6 std::cout << std::endl; return 0; }3.4 Access Functions
- Subscript visit: deq [1] // does not check whether the cross-border
- at ways to access: deq.at (1) // difference is at least two checks for cross-border, is an exception will be thrown out of range
- Access the first element: deq.front ()
- The last element visit: deq.back ()
#include <iostream> #include <deque> int main(int argc, char const* argv[]) { std::deque<int> deq; for (int i = 0; i < 6; i++) deq.push_back(i); // indexed access STD :: COUT << DEQ [ 0 ] STD :: << endl; // Output: 0 // AT ways to access std :: cout << deq.at ( 0 ) << std :: endl; // output: 0 // access the first element of std :: cout << deq.front () << std :: endl; // output: 0 // access the last element of a std :: cout << deq.back () << std :: endl; // output: 5 return 0; }3.5 Other functions
- A plurality of elements assignment: deq.assign (int nSize, const T & x) // assignment with a similar array initialization time
- Two switching elements of the same type of containers: swap (deque &)
#include <iostream> #include <deque> int main ( int argc, char const * the argv []) { // plurality of elements assigned STD :: the deque < int > DEQ; deq.assign(3, 1); deque<int> deq2; deq2.assign(3, 2); // switching elements of two containers deq.swap (deq2); // 遍历显示 std::cout << "deq: "; for (int i = 0; i < deq.size(); i++) std::cout << deq[i] << " "; // 输出:2 2 2 std::cout << std::endl; // 遍历显示 std::cout << "deq2: "; for (int i = 0; i < deq2.size(); i++) std::cout << deq2[i] << " "; // 输出:1 1 1 std::cout << std::endl; return 0; }
Fourth, iterators and algorithms
4.1 iterator
- Iterating pointer: deq.begin ()
- Iterator end pointer: deq.end () // points to the next position of the last element
- Iterator pointing to the beginning of the constant pointer: deq.cbegin () // meaning that they can not pass this pointer to modify the contents referred to, but still can be modified by other means, and can also move the pointer.
- End of the constant point iterator pointer: deq.cend ()
- Iterator reverse pointer to the last element: deq.rbegin ()
- Iterator reverse pointer to the previous element of the first element: deq.rend ()
#include <iostream> #include <deque> int main(int argc, char const* argv[]) { std::deque<int> deq; deq.push_back(1); deq.push_back(2); deq.push_back(3); std::cout << *(deq.begin()) << std::endl; // 输出:1 std::cout << *(--deq.end()) << std::endl; // 输出:3 std::cout << *(deq.cbegin()) << std::endl; // 输出:1 std::cout << *(--deq.cend()) << std::endl; // 输出:3 std::cout << *(deq.rbegin()) << std::endl; // 输出:3 std::cout << *(--deq.rend()) << std::endl; // 输出:1 std::cout << std::endl; return 0; }4.2 algorithm
- Traversing element
std::deque<int>::iterator it; for (it = deq.begin(); it != deq.end(); it++) std::cout << *it << std::endl; // 或者 for (int i = 0; i < deq.size(); i++) { std::cout << deq.at(i) << std::endl; }
- Flip element
#include <algorithm> reverse(deq.begin(), deq.end());
- Sort elements
#include <algorithm> :: the Sort std (deq.begin (), deq.end ()); // is used in small to large sort // if you want descending order, after the first sorting inversion methods can be used, the following method may be employed: // custom descending comparator for changing the ordering BOOL Comp ( const int & A , const int & B) { return A> B; } std::sort(deq.begin(), deq.end(), Comp);
V. Summary
You can see, deque and vector usage are basically the same, except for the following few differences:
- deque no capacity () function, and the vector have;
- There push_front deque () and pop_front () function, and no vector;
- deque no data () function, and the vector have.
----------------
Disclaimer: This article is cnblog blogger " fengMisaka " article, reproduced, please attach the original source link and this statement.
Original link: https://www.cnblogs.com/linuxAndMcu/p/10260124.html