STL---A small summary of various common functions in STL _ Data structure

Source: Internet
Author: User

STL = Standard Template library, Standard template gallery, HP Labs developed a series of software collectively. It was developed by Alexander Stepanov, Meng Lee and David R Musser at HP Labs. This is probably the most boring term for one of the most exciting tools in history. Fundamentally, STL is a collection of "containers", these "containers" have List,vector,set,map, STL is also the algorithm and some other components of the collection. Here's a collection of "containers" and algorithms that refer to the masterpieces of many of the world's most intelligent people for many years. The purpose of the STL is to standardize components so that there is no need for redevelopment to use off-the-shelf components. STL is now part of C + +, so there is no need to install anything extra.

Vectors (vector) continuously stored elements <vector> lists (list) are a two-way chain of nodes, each of which contains an array of elements <list> two-queue (deque) sequentially stored pointers to different elements < The deque> set (set) is a red-black tree of nodes, each containing an element that is arranged between nodes in a predicate that acts on the pairs of elements, and no two different elements can have the same order <set> multiple sets (Multiset) Arrangement of sets <set> stack (stack) LIFO values that allow two order of equality <stack> queue (queue) FIFO value arrangement <queue> precedence queue (priority_queue) The order of an element is a set of queues <queue> mappings (map) that is determined by a predicate acting on the stored value pair, by {key, value} pairs, with a function of Yu Yu predicate alignment <map> multiple mappings (multimap) Mappings that allow the key pairs to have an equal order <map>

-------excerpted from Baidu Encyclopedia


The header files commonly used in STL:

<span style= "FONT-SIZE:18PX;" > #include <iostream>
#include <stdio.h>
#include <string.h>
#include < algorithm>//algorithm
#include <queue>//queue
#include <map>
#include <deque>//two-terminal Queue
# Include <vector>
#include <stack>//stack
#include <iterator>//iterator </span>


First, priority queue

The Priority_queue template class has three template parameters, which are described in the following two ways:


Element type
priority_queue<int>q;

Container type
//1. From small to large
priority_queue<int,vector<int>,greater<int> >q;
2. From big to small
priority_queue<int,vector<int>,less<int> >q;


Priority queues, using the example:

Priority_queue<int,vector<int>,greater<int> >q;
        for (int i=0;i<10;i++)
        {
            scanf ("%d", &a);
            Q.push (a);
        }
        
        while (!q.empty ())
        {
            x = Q.top ();
            Q.pop ();
            if (Q.empty ()) break
                ;
            printf ("%d", x);
        }

such as the above code, if input: 1,5,4,7,2,9,8,3,10,6 This random arrangement is the number of

Output, it will output directly: 1,2,3,4,5,6,7,8,9,10

If you define it, change the greater into less,

Output, it will output directly: 10,9,8,7,6,5,4,3,2,1

This is the benefit of the priority queue I understand (PS: There may be other, if I know I will add).



Q.push (a); will be pressed into the priority queue Q, after the press is also directly to the order should be in place.

To access the queue first element q.top ();

Out queue: Q.pop ();

Into the queue: Q.push (x);

Determines whether the priority queue is empty: Q.empty ();

(The following map, with set from God)

Second, map

A map is a collection of key-value pairs. The map type is generally understood to be an associative array.


A classic dictionary tree, written in a dictionary tree, about 90 lines, with map as long as 40 is enough,

Yes, map is so easy to use,


Map header file: #include <map>;

The definition of the Map object:

map<string,int>q;
 map<int,int>q;
 map<string,node>q;
 map<int,node>;
 map<int,string>q;


Map add element:

Such as:

map<int,string>q;
 Q[100]= "adnsnd";
can also:
Q.insert (pair<int,string>, "adnsnd"));
Q.insert (Map<int,string>::value_type, "adnsnd"));


Map to find and read elements:

such as map<int,string>q;

The simplest method: int n=q["Dadad"];

Q.count (x); Returns the number of times x appears in Q.

To determine if x appears in Q:

if (Q.find (x) ==q.end ())//x is not present in Q.

Use iterators to determine:

Map<int,string>::iterator it=q.find (x);

if (It!=q.end ())//x appears in Q.

Delete element in map:

Q.erase (x)//delete the element in Q with the key X. Returns the value of the Size_type type, representing the number of deleted elements.

Iterative traversal of a map object:

Map<int,string>::const_iterator It=q.begin ();

while (It!=q.end ())

{

printf ("%d%d\n", it-first,it-second);

it++;

}



Third, set

Header files: #include <set>

Definition of Set Object:set<int>ivec;

add element in set:

Ivec.insert (10);

Get elements in Set

Ivec.find (x);

To determine if X appears in Ivec:

Ivec.find (x);

You can also use Ivec.count (x);

The return value of count here can only be 1 or 0.

The traversal of the set;

Set<int>::iterator It=ivec.begin ();

while (It!=q.end ())

{

printf ("%d", *it);

it++;}

Delete element for set:

It=ivec.find (x);

Ivec.erase (IT);

Use of the lower_bound/upper_bound of set:

Using iterators set<int>::iterator itlow,itup;

Itlow=ivec.lower_bound (x);

Itup=ivec.upper_bound (x);

Lower_bound returns the position of the first number in the Ivec that is greater than or equal to X.

The Upper_bound returns a position that is greater than the first number of x in the Ivec;




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.