fork(1) download
  1. #include <iostream>
  2. #include <vector>
  3. #include <stdio.h>
  4. #include <algorithm>
  5.  
  6.  
  7. using namespace std;
  8.  
  9. struct Point{
  10. int x;
  11. int y;
  12. };
  13.  
  14. struct compare_functor : public binary_function<bool,Point,Point>
  15. {
  16. bool operator ()(const Point &lhs, const Point &rhs) {
  17. return lhs.x<rhs.x ;
  18. }
  19. };
  20.  
  21. int main() {
  22. vector<Point> points;
  23. for (int i = 0; i<100; i++) {
  24. //ランダムな点を生成
  25. Point pt = { rand() % (i+1), 0 };
  26. auto pos=lower_bound(points.begin(),points.end(),pt,compare_functor()) ;
  27. if(pos==points.end())
  28. points.push_back(pt) ;
  29. else
  30. points.insert(pos,pt) ;
  31. }
  32.  
  33. //要素を順に表示
  34. for (int i = 0; i < 100; i++) {
  35. cout << points[i].x << endl;
  36. }
  37.  
  38. return 0;
  39.  
  40. }
Success #stdin #stdout 0s 3472KB
stdin
Standard input is empty
stdout
0
0
0
0
0
0
0
1
1
1
1
1
2
2
2
2
2
3
3
3
3
3
4
5
5
5
6
6
7
7
7
7
7
7
8
8
8
8
9
11
11
11
12
13
14
14
14
14
15
16
16
16
17
18
18
19
19
20
20
20
21
21
21
21
22
23
23
23
24
24
25
26
26
26
27
28
32
32
34
34
36
39
39
43
47
50
51
51
52
53
62
62
63
64
67
68
70
75
93
94