deque of C + + container

catalogue 1.deque container concept 2. Construction of d...
2.1 default construction of deque objects
Parametric construction of 2.2deque objects

catalogue

1.deque container concept

2. Construction of deque object

2.1 default construction of deque objects

Parametric construction of 2.2deque objects

3. Addition and removal of deque head and end

4. Data access of deque

5.deque and iterator

6. Assignment of deque

7. Size of deque

8. Insertion of deque

9. Deletion of deque

1.deque container concept

deque container concept

Deque is the abbreviation of "double ended queue". Like vector, deque is the container of STL. The only difference is:

deque is a two ended array and vector is a single ended array.

  Deque features:

  • deque is very similar to vector in interface and can be directly replaced in many operations.
  • deque can access elements randomly (supports direct access to index values, using [] operator or at() method)
  • deque adds or removes elements from the head and tail very quickly, but inserting or removing elements in the middle is time-consuming.

When used, include header files: #include < deque >  

2. Construction of deque object

2.1 default construction of deque objects

deque is also implemented using template classes.

Default construction form of deque object: deque < T > deqt

For example:

deque <int> deqInt;            // Deque container for int.

deque <float> deqFloat;         // A deque container for storing floats.

deque <student> deqStu;        // Deque container for students.

...

Note: pointer type or custom type can also be set in angle brackets.  

Parametric construction of 2.2deque objects

Mode 1: deque(beg,end);    // The constructor copies the elements in the [beg, end) interval to itself.

Mode 2: deque(n,elem);   // The constructor copies n elems to itself.

Mode 3: deque(const deque)  & deq);  // Copy constructor.

deque<int> deqIntA; deqIntA.push_back(1); deqIntA.push_back(2); deqIntA.push_back(3); deqIntA.push_back(4); deque<int> deqIntB(deqIntA.begin(),deqIntA.end()); //1 2 3 4 deque<int> deqIntC(8, 666); //8 8 8 8 8 deque<int> deqIntD(deqIntA); //1 2 3 4
3. Addition and removal of deque head and end

1.deque.push_back(element); / / add a data at the end of the container

2.deque.push_front(element); / / insert a data into the container head

3.deque.pop_back();         // Delete the last data in the container

4.deque.pop_front();     // Delete container first data

deque<int> deqIntA; deqIntA.push_back(1); deqIntA.push_back(2); deqIntA.push_back(3); deqIntA.push_back(4); deqIntA.push_back(5); deqIntA.push_back(6); deqIntA.pop_front(); deqIntA.pop_front(); deqIntA.push_front(7); deqIntA.push_front(8); deqIntA.pop_back(); deqIntA.pop_back(); deqIntA Remaining elements in: 8 7 3 4
4. Data access of deque

first   Use subscript operation deqIntA[0] = 100;

second   Use the at method, such as deqIntA.at(2) = 100;

third   Interface returns references deqIntA.front() and deqIntA.back()  

be careful:    The first and second methods must pay attention to cross-border

For example: deque<int> deqIntA; deqIntA.push_back(1); deqIntA.push_back(2); deqIntA.push_back(3); deqIntA.push_back(4); deqIntA.push_back(5); int i1 = deqIntA.at(0); //i1 = 1 int i2 = deqIntA[1]; //i2 = 2 deqIntA.at(0) = 666; //Change the first element to 666 deqIntA[1] = 888; //Change the second element to 888 int iFront = deqInt.front(); //666 int iBack = deqInt.back(); //5 deqInt.front() = 888; //Change the first element to 888 deqInt.back() = 666; //Change the last element to 666
5.deque and iterator

1.deque.begin();  // Returns the iterator for the first element in the container.

2.deque.end();   // Returns the iterator after the last element in the container.

3.deque.rbegin();  // Returns the iterator of the penultimate element in the container.

4.deque.rend();   // Returns the iterator after the penultimate element in the container.

5.deque.cbegin();  // Returns the constant iterator for the first element in the container.

6.deque.cend();   // Returns the constant iterator after the last element in the container.

deque<int> deqIntA; deqIntA.push_back(1); deqIntA.push_back(2); deqIntA.push_back(3); deqIntA.push_back(4); deqIntA.push_back(5); //Ordinary iterator for(deque<int>::iterator it = deqIntA.begin(); it!=deqIntA.end(); ++it){ (*it)++; //*it++ (*it)++ cout<<*it; cout<<" "; } //Constant iterator deque<int>::const_iterator cit = deqIntA.cbegin(); for( ; cit!=deqIntA.cend(); cit++){ cout<<*cit; cout<<" "; } //Inverted iterator for(deque<int>::reverse_iterator rit=deqIntA.rbegin(); rit!=deqIntA.rend(); ++rit){ cout<<*rit; cout<<" "; }
6. Assignment of deque

1.deque.assign(beg,end);    // Assign the data copy in the [beg, end) interval to itself. Note that the interval is left closed and right open.

2.deque.assign(n,elem);  // Assign n elem copies to itself.

3. Deque & operator = (const deque & DEQ); / / overload the equal sign operator

4.deque.swap(deq);  // Swap deque with its own elements

For example: deque<int> deqIntA,deqIntB,deqIntC,deqIntD; deque<int> deqIntA; deqIntA.push_back(1); deqIntA.push_back(2); deqIntA.push_back(3); deqIntA.push_back(4); deqIntA.push_back(5); deqIntB.assign(deqIntA.begin(),deqIntA.end()); // 1 2 3 4 5 deqIntC.assign(4,888); //888 888 888 888 deqIntD = deqIntA; //1 2 3 4 5 deqIntC.swap(deqIntD); //exchange
7. Size of deque

1.deque.size();     // Returns the number of elements in the container

2.deque.empty();     // Determine whether the container is empty

3.deque.resize(num);    // Reassign the length of the container to num. if the container becomes longer, the new position will be filled with the default value of 0. If the container becomes shorter, the elements whose end exceeds the length of the container will be deleted.

4.deque.resize(num, elem);  // Reassign the length of the container to num. if the container becomes longer, fill the new position with the elem value. If the container becomes shorter, the elements whose end exceeds the length of the container are deleted.

deque<int> deqIntA; deqIntA.push_back(1); deqIntA.push_back(2); deqIntA.push_back(3); deqIntA.push_back(4); deqIntA.push_back(5); int iSize = deqIntA.size(); //5 deqIntA.resize(7); //1 2 3 4 5 0 0 deqIntA.resize(8,1); //1 2 3 4 5 0 0 1 deqIntA.resize(2); //1 2
8. Insertion of deque

1.deque.insert(pos,elem);   // Insert a copy of the element in the POS position and return the location of the new data.

2.deque.insert(pos,n,elem);   // Insert n elem data in POS position, no return value.

3.deque.insert(pos,beg,end);   // Insert the data in the [beg, end) interval at the POS position, and there is no return value

9. Deletion of deque

1.deque.clear();      // Remove all data from the container

2.deque.erase(beg,end); / / delete the data in the [beg,end) interval and return the location of the next data.

3.deque.erase(pos);    // Delete the data in POS position and return the position of the next data.

30 October 2021, 11:53 | Views: 2766

Add new comment

For adding a comment, please log in
or create account

0 comments