fork(2) download
  1. #include <iostream>
  2. #include <stdlib.h>
  3. #include <stdio.h>
  4. #include <bits/stl_tree.h>
  5. using namespace std;
  6.  
  7. struct Node
  8. {
  9. Node(int id):id_(id) {};
  10. struct Key
  11. {
  12. int operator()(const Node& node) {return node.id_;}
  13. };
  14.  
  15. int id_;
  16. };
  17. struct MyCmp
  18. {
  19. bool operator()(int a,int b) {printf("MyCmp:%d %d\n",a,b); return a<b; }
  20. };
  21.  
  22. template<typename K,typename V,typename KofV,typename Comp>
  23. //class MyTree : public _Rb_tree<int,int,_Identity<int>, less<int> >
  24. class MyTree: public _Rb_tree<K,V,KofV,Comp>
  25. {
  26. public:
  27. using _Rb_tree<K,V,KofV,Comp>::_M_insert_unique;
  28. using _Rb_tree<K,V,KofV,Comp>::iterator;
  29. };
  30.  
  31.  
  32. int main() {
  33. //MyTree<int,int,_Identity<int>, less<int> > tree1;
  34. MyTree<int, Node, Node::Key, less<int> > tree1;
  35. for (int idx = 30; idx>= 0; --idx)
  36. {
  37. tree1._M_insert_unique( Node(idx) );
  38. }
  39. MyTree<int, Node, Node::Key, less<int> >::iterator it = tree1.begin();
  40. for(; it!=tree1.end();++it)
  41. {
  42. printf("Tree1:%d\n", (*it).id_);
  43. }
  44.  
  45. printf("=============test another kind of tree==========\n");
  46. MyTree<int, Node, Node::Key, MyCmp > tree2;
  47. for (int idx = 30; idx>= 0; --idx)
  48. {
  49. tree2._M_insert_unique(Node(idx));
  50. }
  51. MyTree<int, Node, Node::Key, MyCmp >::iterator it2 = tree2.begin();
  52. for(; it2!=tree2.end();++it2)
  53. {
  54. printf("Tree2:%d\n", (*it2).id_);
  55. }
  56. return 0;
  57. }
Success #stdin #stdout 0s 3428KB
stdin
Standard input is empty
stdout
Tree1:30
Tree1:29
Tree1:28
Tree1:27
Tree1:26
Tree1:25
Tree1:24
Tree1:23
Tree1:22
Tree1:21
Tree1:20
Tree1:19
Tree1:18
Tree1:17
Tree1:16
Tree1:15
Tree1:14
Tree1:13
Tree1:12
Tree1:11
Tree1:10
Tree1:9
Tree1:8
Tree1:7
Tree1:6
Tree1:5
Tree1:4
Tree1:3
Tree1:2
Tree1:1
=============test another kind of tree==========
MyCmp:29 0
MyCmp:0 29
MyCmp:29 0
MyCmp:28 0
MyCmp:28 0
MyCmp:0 28
MyCmp:28 0
MyCmp:27 0
MyCmp:27 0
MyCmp:0 27
MyCmp:27 0
MyCmp:26 0
MyCmp:26 0
MyCmp:26 0
MyCmp:0 26
MyCmp:26 0
MyCmp:25 0
MyCmp:25 0
MyCmp:25 0
MyCmp:0 25
MyCmp:25 0
MyCmp:24 0
MyCmp:24 0
MyCmp:24 0
MyCmp:24 0
MyCmp:0 24
MyCmp:24 0
MyCmp:23 0
MyCmp:23 0
MyCmp:23 0
MyCmp:23 0
MyCmp:0 23
MyCmp:23 0
MyCmp:22 0
MyCmp:22 0
MyCmp:22 0
MyCmp:22 0
MyCmp:0 22
MyCmp:22 0
MyCmp:21 0
MyCmp:21 0
MyCmp:21 0
MyCmp:21 0
MyCmp:0 21
MyCmp:21 0
MyCmp:20 0
MyCmp:20 0
MyCmp:20 0
MyCmp:20 0
MyCmp:20 0
MyCmp:0 20
MyCmp:20 0
MyCmp:19 0
MyCmp:19 0
MyCmp:19 0
MyCmp:19 0
MyCmp:19 0
MyCmp:0 19
MyCmp:19 0
MyCmp:18 0
MyCmp:18 0
MyCmp:18 0
MyCmp:18 0
MyCmp:18 0
MyCmp:0 18
MyCmp:18 0
MyCmp:17 0
MyCmp:17 0
MyCmp:17 0
MyCmp:17 0
MyCmp:17 0
MyCmp:0 17
MyCmp:17 0
MyCmp:16 0
MyCmp:16 0
MyCmp:16 0
MyCmp:16 0
MyCmp:16 0
MyCmp:16 0
MyCmp:0 16
MyCmp:16 0
MyCmp:15 0
MyCmp:15 0
MyCmp:15 0
MyCmp:15 0
MyCmp:15 0
MyCmp:15 0
MyCmp:0 15
MyCmp:15 0
MyCmp:14 0
MyCmp:14 0
MyCmp:14 0
MyCmp:14 0
My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