fork(2) download
  1. #include <algorithm>
  2. #include <iterator>
  3. #include <iostream>
  4. #include <iomanip>
  5. #include <utility>
  6. #include <string>
  7. #include <vector>
  8. #include <memory>
  9. #include <limits>
  10. #include <queue>
  11. #include <cmath>
  12. #include <map>
  13. #include <set>
  14.  
  15. using namespace std;
  16.  
  17. typedef long long ll;
  18.  
  19. struct document
  20. {
  21. float rank;
  22. document(float rank):
  23. rank(rank)
  24. { }
  25. };
  26.  
  27. using doc_ptr = shared_ptr<document>;
  28.  
  29. bool operator < (const doc_ptr& x, const doc_ptr& y)
  30. {
  31. return x->rank < y->rank;
  32. }
  33.  
  34. int main()
  35. {
  36. int n = 10;
  37. auto cmp_by_rank = [](const doc_ptr& x, const doc_ptr& y) { return x->rank < y->rank; };
  38. auto cmp_by_ptr = [](const doc_ptr& x, const doc_ptr& y) { return x.get() < y.get(); };
  39.  
  40. vector<doc_ptr> v;
  41.  
  42. for(int i = 0; i < n; i++) {
  43. float r = static_cast <float> (rand()) / static_cast <float> (RAND_MAX);
  44. v.emplace_back(make_shared<document>(r));
  45. }
  46.  
  47. sort(v.begin(), v.end());
  48.  
  49. for(const auto& doc: v)
  50. cout << doc->rank << " " << uint64_t(doc.get()) << endl;
  51.  
  52. cout << "v is sorted = " << std::is_sorted(v.begin(), v.end()) << endl;
  53. cout << "v is sorted (by rank): " << is_sorted(v.begin(), v.end(), cmp_by_rank) << endl;
  54. cout << "v is sorted (by ptr): " << is_sorted(v.begin(), v.end(), cmp_by_ptr) << endl;
  55. cout << "----" << endl;
  56.  
  57. priority_queue<doc_ptr> pq;
  58. for(int i = 0; i < n; i++) {
  59. float r = static_cast <float> (rand()) / static_cast <float> (RAND_MAX);
  60. pq.push(make_shared<document>(r));
  61. }
  62.  
  63. vector<doc_ptr> pq_v;
  64.  
  65. while(!pq.empty()) {
  66. cout << pq.top()->rank << " " << uint64_t(pq.top().get()) << endl;
  67. pq_v.push_back(pq.top());
  68. pq.pop();
  69. }
  70.  
  71. cout << "pq_v is sorted: " << is_sorted(pq_v.rbegin(), pq_v.rend()) << endl;
  72. cout << "pq_v is sorted (by rank): " << is_sorted(pq_v.rbegin(), pq_v.rend(), cmp_by_rank) << endl;
  73. cout << "pq_v is sorted (by ptr): " << is_sorted(pq_v.rbegin(), pq_v.rend(), cmp_by_ptr) << endl;
  74.  
  75. return 0;
  76. }
  77.  
Success #stdin #stdout 0s 4536KB
stdin
Standard input is empty
stdout
0.197551 94882010476000
0.277775 94882010476096
0.335223 94882010476032
0.394383 94882010475632
0.55397 94882010475856
0.76823 94882010476064
0.783099 94882010475600
0.79844 94882010475792
0.840188 94882010475568
0.911647 94882010475824
v is sorted = 1
v is sorted (by rank): 1
v is sorted (by ptr): 0
----
0.141603 94882010480816
0.717297 94882010480784
0.635712 94882010480752
0.916195 94882010480720
0.606969 94882010480576
0.95223 94882010480544
0.513401 94882010480512
0.628871 94882010475952
0.364784 94882010475920
0.477397 94882010475888
pq_v is sorted: 0
pq_v is sorted (by rank): 0
pq_v is sorted (by ptr): 1