Pilot exercises summarize 03:c++ STL Preliminary

Source: Internet
Author: User
Tags set set

Note Content Source: "Algorithmic Competition Classic Primer" (second edition) Rujia the 5th Chapter

First, the basic concept

STL refers to the standard template Library of C + +.

Second, sort and search

The sort function allows you to sort any object , not necessarily a built-in type.

If you want to sort by the sort function, this type needs to define the "less than" operator, or pass in a "less than" function when sorting . A sorted object can exist in an ordinary array or vector. The normal array is called with sort (A, a+n), which is called with sort (V.begin (), V.end ()).

Sort is a template function in the algorithm header file.

Three, indefinite long array vector

If A is a vector:

A.size () is the array size

A.resize () is a change in size

A.push_back () is adding elements to the tail

A.pop_back () is the last element removed

Declaration method:vector<int> A;

Iv. Set Set

Each element appears at most once

You can construct a set with a custom type, and you need to define a "less than" operator.

Set has the properties in which elements are raised from small to large .

Statement:set<string> Dict;

Dict.begin (), Dict.end ()

The concept of a set iterator (similar to a For loop)

 for (set<string>::iterator it = Dict.begin (); It!=dict.end (); + +it)    cout<<* it<<"\ n";

Five, mapping map

A map is a mapping from key to value, and the[] operator is overloaded .

Statement: map<string, int> month_name;

month_name["July"] = 7;

Both set and map support operations such as INSERT, find, count, remove, and can iterate through the elements in small to large order.

Six, stack, queue and priority queue

1. Stack

Stack definition in header file <stack>

Declaration way:stack<int> S;

Push (), pop ().

2. Queues

Queue definition in header file <queue>

Declaration Way:queue<int> Q;

Push (), pop (), front () take the first element of the queue but do not delete it.

3. Priority queue

A priority queue is an abstract data type that behaves a bit like a queue.

But the first-out queue is not an element of the advanced queue, but the highest-priority element in the queue .

Priority queue is also defined in header file <queue>

Declaration Mode:priority_queue<int> PQ; ===> This PQ is a "smaller priority queue with lower priority for integers".

Custom types can also form a priority queue, but you must define a priority for each element. This priority does not require a definite number, only the size can be compared to the line.

You can define a struct CMP, overload the "()" operator, make it look like a function, and then declare the priority queue as follows:

Priority_queue<int, Vector<int>, Cmp> PQ;

CMP can be defined in the following form:

struct cmp{      booloperator() (constintconstint  Const  {             //A has a priority greater than B-hour return truereturn             10 ;        }    }    

Other common priority queues are:

Priority queue for "smaller integers with higher priority": Priority_queue<int, Vector<int>, greater<int> > PQ;

Push (), pop (), Top () takes the first element of the queue (but does not delete it).

Pilot exercises summarize 03:c++ STL Preliminary

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.