Double-end queues and table containers of STL containers

Source: Internet
Author: User

STLYesC ++Class Library. Containers in STL have queues.ContainerAnd associated containers, container adapter congtainer adapters: stack, queue, priority queue), bit_set, and string_package.

In the series, I will introduce the list, vector, deque and other queue containers, and association containers such as set, multisets, map, and multimaps. There are a total of seven basic container classes.

Queue container ordered container): the queue container stores the set of T-type values in a linear arrangement. Each member of the queue has its own unique position. Ordered containers include vector type, double-end queue type, and list type.

The following describes the two-end queue containers and table containers.

Double-end queue deque container class ):

Deque pronunciation: deck, meaning: double queue, # include <qeque>) the container class is similar to the vector class and supports random access and Quick insertion and deletion, it takes linear time to operate at a certain position in the container. Unlike vector, deque also supports data insertion from the start end: push_front ().

In addition, deque does not support operations similar to capacity () and reserve () of vector.

 
 
  1. # Include <iostream>
  2. # Include <deque>
  3. Using namespace std;
  4. Typedef deque <int> INTDEQUE; // some people hate this definition.
  5. // Display all the elements of the deque queue from the front to the back
  6. Void put_deque (INTDEQUE deque, char * name)
  7. {
  8. INTDEQUE: iterator pdeque; // still use iterator output
  9. Cout <"The contents of" <name <":";
  10. For (pdeque = deque. begin (); pdeque! = Deque. end (); pdeque ++)
  11. Cout <* pdeque <"; // note that there is a" * "number. If there is no" * "number, an error is returned.
  12. Cout <endl;
  13. }
  14. // Test the deqtor container Function
  15. Int main ()
  16. {
  17. // The deq1 object is initially empty.
  18. INTDEQUE deq1;
  19. // The deq2 object initially has 10 elements with a value of 6.
  20. INTDEQUE deq2 (10, 6 );
  21. // Declare a bidirectional iterator variable named I
  22. INTDEQUE: iterator I;
  23. // Display data in deq1 from the beginning to the back
  24. Put_deque (deq1, "deq1 ");
  25. // Display data in deq2 from the beginning to the back
  26. Put_deque (deq2, "deq2 ");
  27. // Add two elements to the deq1 Sequence
  28. Deq1.push _ back (2 );
  29. Deq1.push _ back (4 );
  30. Cout <"deq1.push _ back (2) and deq1.push _ back (4):" <endl;
  31. Put_deque (deq1, "deq1 ");
  32. // Add two elements from the front of the deq1 Sequence
  33. Deq1.push _ front (5 );
  34. Deq1.push _ front (7 );
  35. Cout <"deq1.push _ front (5) and deq1.push _ front (7):" <endl;
  36. Put_deque (deq1, "deq1 ");
  37. // Insert data in the middle of the deq1 Sequence
  38. Deq1.insert (deq1.begin () + 1, 3, 9 );
  39. Cout <"deq1.insert (deq1.begin () + 1, 3, 9):" <endl;
  40. Put_deque (deq1, "deq1 ");
  41. // Test the reference class function
  42. Cout <"deq1.at (4) =" <deq1.at (4) <endl;
  43. Cout <"deq1 [4] =" <deq1 [4] <endl;
  44. Deq1.at (1) = 10;
  45. Deq1 [2] = 12;
  46. Cout <"deq1.at (1) = 10 and deq1 [2] = 12:" <endl;
  47. Put_deque (deq1, "deq1 ");
  48. // Remove an element from the front and back of the deq1 Sequence
  49. Deq1.pop _ front ();
  50. Deq1.pop _ back ();
  51. Cout <"deq1.pop _ front () and deq1.pop _ back ():" <endl;
  52. Put_deque (deq1, "deq1 ");
  53. // Clear the 2nd elements in deq1
  54. Deq1.erase (deq1.begin () + 1 );
  55. Cout <"deq1.erase (deq1.begin () + 1):" <endl;
  56. Put_deque (deq1, "deq1 ");
  57. // Assign values to deq2 and display them
  58. Deq2.assign (8, 1 );
  59. Cout <"deq2.assign (8, 1):" <endl;
  60. Put_deque (deq2, "deq2 ");
  61. }

The above demonstrates how deque performs insert and delete operations, such as erase () and assign (), which are available in most containers. For more information about deque, see deque.

Table List container class)

List # include <list>) is also called a linked list. It is a bilinear List and can only be accessed from the front to the back or back to the back in sequence. Figure 2 shows the data structure of the list. One obvious difference from the previous two container classes is that they do not support random access. To access the items at a subscript in a table, you need to start the loop from the header or end of the table to the end of the subscript. The subscript budget operator [] is missing.

At the same time, the list still contains basic functions such as erase (), begin (), end (), insert (), push_back (), and push_front, next we will demonstrate other functions of list.

  • Merge (): merges two sort lists;
  • Splice (): concatenates two lists;
  • Sort (): sort the list

 
 
  1. # Include <iostream>
  2. # Include <string>
  3. # Include <list>
  4. Using namespace std;
  5. Void PrintIt (list <int> n)
  6. {
  7. For (list <int >:: iterator iter = n. begin (); iter! = N. end (); ++ iter)
  8. Cout <* iter <"; // use the iterator for output Loop
  9. }
  10. Int main ()
  11. {
  12. List <int> listn1, listn2; // initialize listn1 and listn2
  13. Listn1.push _ back (123 );
  14. Listn1.push _ back (0 );
  15. Listn1.push _ back (34 );
  16. Listn1.push _ back (1123); // now listn1 :,
  17. Listn2.push _ back (100 );
  18. Listn2.push _ back (12); // now listn2: 12,100
  19. Listn1.sort ();
  20. Listn2.sort (); // sorts listn1 and listn2
  21. // Now listn1: 12,100, listn2:
  22. PrintIt (listn1 );
  23. Cout <endl;
  24. PrintIt (listn2 );
  25. Listn1.merge (listn2); // after merging two sort lists, listn1: 34,100,123,112, 3
  26. Cout <endl;
  27. PrintIt (listn1 );
  28. }

The above section does not demonstrate the usage of the splice () function. It is a little troublesome to use. Figure 3 shows the splice function. Insert a list to another list. The list container class defines three versions of the splice () function:

 
 
  1. splice(position,list_value);  
  2. splice(position,list_value,ptr);  
  3. splice(position,list_value,first,last); 

Ist_value is an existing list and will be inserted into the source list. position is an iteration parameter, which currently points to a specific position in the list to be spliced.

Listn1: 12,100, listn2:

Execute listn1.splice (find (listn1.begin (), listn1.end (), 0), listn2); then, listn1 will change to 1123. Insert listn2 to the element 0 of listn1. The find () function finds the position of the element 0 in listn1. It is worth noting that after the splice is executed, the list_value will no longer exist. In this example, listn2 will no longer exist.

In the second version, ptr is an iterator parameter. The execution result is to insert the value pointed to by ptr directly before the position currently pointed. this inserts only one element into the source list.

The first and last versions of the third version are also iterator parameters and are not equal to list_value.begin (), list_value.end (). First refers to the First element of the column to be inserted, and last refers to the last element of the column to be inserted.

If listn1: 123,0, 34,1123 listn2: 12,43, 87,100, after the following functions are executed

Listn1.splice (find (listn1.begin (), listn1.end (), 0), ++ listn2.begin (), -- listn2.end ());

Listn1: 123,43, 87,0, 34,1123 listn2: 12,100

As mentioned above, we have learned about two basic ordered containers: deque and list. Other ordered containers include slist and bit_vector. Continue to the next article>

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.