STL && usage collection

..... STL is very strong in c ++ good use of a series of containers (function) and the like, until not very good, so always died suddenly. . . . Thinking fast game, and it's time to reason about these things.

-1、pair

Storing things two basic elements

0, overloaded operator (for heavy-duty structure)

struct Node 
{ 
    int A; 
return type operator symbol ( const Node just a name) const 
    { 
        operations, a is another structure (passed in), one internal element 
    } 
};
View Code

+ For example:

And returns two elements

#include<bits/stdc++.h>
using namespace std;
struct node
{
    int a,b;
    node operator + (const node x)const
    {
        return a+x.a;
    }
};
int main()
{
    node x={1,2};
    node y={2,3};
    int c=x+y;
    printf("%d",c);
    return 0;
}
View Code

1, queue (queue) (header file: queue) (used for wide search)

The first course was a queue.

Definition method:

queue <Type> Name

Wherein the type can be all sorts of things, including the structure, pair, and the like.

Usage: all in the name of the queue q

q.push (A); // plug element to the tail of the queue A 
q.pop (); // elastic element out queue 
q.front () // the return value of the head elements 
q.size () // return queue length (number of elements) 
q.emoty () // whether the queue is empty
View Code

2, deque (deque) (header: deque (this thing is also a header file ....)) (SPFA used for optimization of the SLF)

Definition method:

deque <Type> Name

Above, there are many types.

Use :( named q)

q.push_back (A); // to plug an element of the tail A 
q.push_front (A); // to head a team element plugs A 
q.pop_back (); // delete the tail element 
q.pop_front (); // delete the head elements 
q.front () // returns the tail element 
q.back () // return the head elements 
q.empty () // determines whether the queue is empty, then when empty, else 0 
Q .size () // returns the queue length 
q.clear () // empty queue
View Code

3, priority_queue (priority queue, stack) (header: queue)

c ++ STL powerful first experience

For maintaining a sequence size can be understood as automatic sorting, is essentially a binary heap, strict adherence to the inside top of the stack the maximum priority (i.e. a minimum value, a large heap root)

Definition method:

priority_queue <Type> Name

Note: this only seems to define a large root heap (put priority on the list, which is a large value back row), node to override operator || cmp (OK or overloaded operators), pair of words with automatic sorting of the value of the first element

Broader wording (easier to use, that is, their own definition of priority)

struct CMP 
{ 
    BOOL  operator () ( int A, int B) 
    { 
        return A <B; // wanted to do something 
    } 
}; 
The priority_queue < int , Vector < int >, CMP> Q;
View Code

Talk about the basic operations:

q.push (A); // plug A 
q.pop (); // shells HOL, i.e. top of the stack 
q.top (); // Returns the top of the stack value 
q.empty () // supra 
q.size () // queue length
View Code

 

Guess you like

Origin www.cnblogs.com/ajmddzp/p/11750089.html