C + + individual container comparison (vector,deque,list,set,map,queue,stack)

Source: Internet
Author: User

1, vector (continuous space storage, you can use the [] operator) can quickly access random elements, quickly inserted at the end of the element, but in the middle of the sequence randomly inserted , delete elements are slow. Also, if there is not enough space at the outset, there is a process to reallocate more space.

2, deque (small piece of continuous, small sheet connected with the linked list, in fact there is a map of the pointer, because know the type, so still can use [], just speed without vector fast) quick access to random elements, fast at the beginning and the end InsertElements. Random. InsertDelete elements slowly, after the space of the newly allocated space, the original elements do not need to backup. For the deque, you can copy the deque to the vector first, sort and then copy back to Deque 3, list (linked to each element with linked list) to access the random element without vector fast, randomly Insertelements are faster than vectors, space is allocated for each element, there is no space, and redistribution occurs. 4, set internal elements unique, with a balanced tree structure to store, so the time to sort the traversal, find also relatively fast 5, map one-to-one mapping, key does not repeat 6, QueueThe statement Queue<int,deque<int> > S is a restricted queue, the storage space is determined by the container of the second parameter, the first parameter, in the case of no second argument, determines the storage space type, and the second argument exists, the first type argument is invalid. The deque type is by default, so you can declare this Queue<int> S1, so that the declared queue storage space is the default deque. Other than that QueueThe second parameter can be selected for the container type including deque, list, and the rest if declared, the operation is limited. 7, Stack's default storage space is also deque, other statements and QueueAlmost, the types of containers that can be used include deque, vectors, and list,stack are advanced post-stack.

1. Stack
The stack template class is defined in the <stack> header file.
The stack template class requires two template parameters, one element type, one container type, but only the element type is necessary
, the default container type is deque when you do not specify a container type.
The sample code that defines the stack object is as follows:
stack<int> S1;
Stack<string> S2;
The basic operations of Stack are:
Into the stack, as in example: S.push (x);
Out of the stack, as in example: S.pop (); Note that the stack operation simply deletes the top element of the stack and does not return the element.
Access to the top of the stack, as in example: S.top ()
Determine the stack null, as an example: S.empty (), returns True when the stack is empty.
Access the number of elements in the stack, such as: S.size ().

2, queue
queue template class is defined in <queue template class also requires two template parameters, one element type, one container class
type, the element type is necessary, the container type is optional , the default is Deque type.
Define queue <int> Q1;
queue <double> Q2;

The basic operations of the queue are:
Queue, as in example: Q.push (x); Connect X to the end of the queue.
Out of the team, as in Example: Q.pop (); The first element of the popup queue, note that the value of the popup element is not returned.
Access the first element of the team, such as: Q.front (), which is the first element to be pressed into the queue.
Access the tail element, such as: Q.back (), which is the element that was last pressed into the queue.
Determine if the queue is empty, as in example: Q.empty (), returns True when the queue is empty.
Access to the number of elements in the queue, as in example: Q.size ()

#include <cstdlib>
#include <iostream>
#include <queue>

using namespace Std;

int Main ()
{
   int e,n,m;
    queue<int> Q1;
   for (int i=0;i<10;i++)
      Q1.push (i);
   if (!q1.empty ())
   cout<< "DUI Lie bu kong\n ";
   n=q1.size ();
   cout<<n<<endl;
   M=q1.back ();
   cout<<m<<endl;
   for (int j=0;j<n;j++)
   {
      E=q1.front ();
      cout<<e<< "";
      Q1.pop ();
   }
   cout<<endl;
   if (Q1.empty ())
   cout<< "DUI Lie bu kong\n ";
   System ("PAUSE");
   return 0;
}

3, Priority_Queue
In <QueueThe > header file also defines another very useful template class Priority_Queue(priority queue). Priority Team
The difference between a column and a queue is that the priority queue is not queued in the queue, but in order of precedence
Out of the queue (the default is the big one, or you can specify your own precedence by assigning operators).
Priority_QueueThe template class has three template parameters, the first one is the element type, the second container type, and the third is a
More operators. The latter two can be omitted, the default container is a vector, the default operator is less, that is, the small forward row, large
The back row (the element out of the queue when the tail is out of the queue).
Define Priority_QueueThe sample code for the object is as follows:
Priority_Queue<int> Q1;
Priority_Queue< Pair<int, int> > Q2; Note Be sure to leave a blank between the two angle brackets.
Priority_Queue<int, Vector<int&gt, greater<int> > Q3; Define a small first-out team
Priority_QueueThe basicOperationAndQueueSame.
Beginners in the use of priority_Queue, the most difficult thing is how to define a comparison operator.
If it is a basic data type, or a class that has a comparison operator defined, you can directly use the less operator of the STL and the greater
Operator-The default is to use the less operator, which is a small forward row, a large first-out team.
If you want to define your own comparison operator, there are several ways to do this: the overloaded comparison operator. Priority Team
The column attempts to take two elements x and y into the comparison operator (to the less operator, call X<y, to the greater operator, call X>y),
If the result is true, X is in front of y, Y will precede X, and vice versa, then Y will precede X and X will first be out of line.
Let's look at this simple example:
#include <iostream>

#include <Queue>
using namespace Std;
Class T
{
Public
int x, y, Z;
T (int A, int b, int c): X (a), Y (b), Z (c)
{
}
};
BOOL operator < (const T &AMP;T1, const T &AMP;T2)
{
return T1.z < t2.z; Determine the order of T1 and T2 in order of Z
}
Main ()
{
Priority_Queue<T> Q;
Q.push (T (4,4,3));
Q.push (T (2,2,5));
Q.push (T (1,5,4));
Q.push (T (3,3,6));
while (!q.empty ())
{
T t = q.top (); Q.pop ();
cout << t.x << "<< t.y <<" << t.z << Endl;
}
return 1;
}
The output is (note that the queue is from large to small in order of Z):
3 3 6
2 2 5
1 5 4
4 4 3
Let's look at an example from small to large teams in Z Order:
#include <iostream>
#include <Queue>
using namespace Std;
Class T
{
Public
int x, y, Z;
T (int A, int b, int c): X (a), Y (b), Z (c)
{
}
};
BOOL operator > (const t &t1, const T &AMP;T2)
{
return t1.z > t2.z;
}
Main ()
{
Priority_Queue<t, Vector<t>, greater<t> > Q;
Q.push (T (4,4,3));
Q.push (T (2,2,5));
Q.push (T (1,5,4));
Q.push (T (3,3,6));
while (!q.empty ())
{
T t = q.top (); Q.pop ();
cout << t.x << "<< t.y <<" << t.z << Endl;
}
return 1;
}
The output is:
4 4 3
1 5 4
2 2 5
3 3 6
If we overload the comparison operator in the first example to:
BOOL operator < (const T &AMP;T1, const T &AMP;T2)
{
return t1.z > t2.z; Determine the order of T1 and T2 in order of Z
}
The first example of the program will get the same output as the program of the second example.

 

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.