Supplements algorithm [4] - STL usage

The main priority queues and strings bb about it. Oh, there is bitset.

Priority Queue

  • It is easy to define: priority_queue<int> pq;
  • The interior is a heap.

Basic Operations

  • pq.top()Take the top of the stack elements; (no front()way!)
  • pq.push(x) insert;
  • pq.pop() Delete (delete the top of the heap);
  • pq.empty() Judgment is empty.

Custom priority

  • The maximum heap: priority_queue<int> pq;
  • Minimum heap: priority_queue< int, vector<int>, greater<int> > pq;
  • There are actually custom-priority cmpmethod (the first priority of the biggest teams):
    Supplements algorithm [4] - STL usage large column ody>
    1
    2
    3
    4
    5
    6
    7
    8
    struct 
    { BOOL operator () ( const int A, const int B) const { return A> B; }};The priority_queue < int , Vector < int >, CMP> PQ; // this time is the minimum heap






example

  • One hundred training 4078: http://bailian.openjudge.cn/practice/4078/

String

The definition easier: string s;

Basic Operations

  • s.size() String length (starting with index 0);
  • s.substr(a, n) Substring configuration, a is the first subscript character, n is an substring character length;
  • s'find(it1, it2, x)Pointer it1and it2find intermediate character x; ( s.find(x)entire slookup x)
  • s.erase(a)Removing elements, A looks like a pointer, and can be findcombined with the removal of the specified character, such as s.erase(std::find(s.begin(), s.end(), ' '));removing any spaces;
  • s.empty() It determines whether null;
  • Support push_backand pop_back;
  • Support +, =and ==operations.

Traversal operation

  • You can auto it = s.begin(); it != s.end(); it++traverse;
  • But I generally use int i = 0; i < s.size(); i++to traverse.

And digital conversion

String-to-digital

  • stoi, stol, stoll: String to Integer;
  • stof, stod, stold: String float switch;

Digital to String

  • to_stringDirect turn into std::string.

Bit vector

definition: bitset<length> b(value);

Basic Operations

  • Bit computing support &, ^, <<, >>and so on;
  • to_string() Into a string;
  • to_ulong(), to_ullong()Is converted to an unsigned integer;
  • flip(i)The i-th bit inverse, index from 0. flip()All bitwise.

Guess you like

Origin www.cnblogs.com/lijianming180/p/12147623.html