C++STLスタックのアルゴリズム

2642 ワード

: vector
1 // range heap example
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28 #include #include #include using   namespace   std;
  int   main () {    int   myints[] = {10,20,30,5,15};    vector< int > v(myints,myints+5);    vector< int >::iterator it;
     make_heap (v.begin(),v.end());    cout << "initial max heap   : "   << v.front() << endl;
     pop_heap (v.begin(),v.end()); v.pop_back();    cout << "max heap after pop : "   << v.front() << endl;
     v.push_back(99); push_heap (v.begin(),v.end());    cout << "max heap after push: "   << v.front() << endl;
     sort_heap (v.begin(),v.end());
     cout << "final sorted range :" ;    for   (unsigned i=0; i " "   << v[i];
     cout << endl;
     return   0; }
:
 
1
2
3
4 initial max heap   : 30 max heap after pop : 20 max heap after push: 99 final sorted range : 5 10 15 20 99
:
http://www.cplusplus.com/reference/algorithm/
http://www.cplusplus.com/reference/algorithm/push_heap/
の :
std::make_Heapは[start,end) をスタックソートし、デフォルトではless、すなわち を に します.
std::pop_heapはfront(すなわち、 の )をendの に し、 りの を しいheapに します.
std::push_Heapは したばかりの( ) をスタックソートします.
std::sort_heapは1つのスタックを べ えて、 に1つの あるシリーズになって、sort_を ることができますheapでは、まず1つのスタック(2つのプロパティ:1、 は1つ の2、 を または して )でなければなりません.そのため、make_を1 する があります.heap.