-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathch10.cpp
More file actions
80 lines (69 loc) · 3.47 KB
/
ch10.cpp
File metadata and controls
80 lines (69 loc) · 3.47 KB
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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
// Copyright (c) 2014 Alexander A. Stepanov and Daniel E. Rose
//
// Permission to use, copy, modify, distribute and sell this software
// and its documentation for any purpose is hereby granted without
// fee, provided that the above copyright notice appear in all copies
// and that both that copyright notice and this permission notice
// appear in supporting documentation. The authors make no
// representations about the suitability of this software for any
// purpose. It is provided "as is" without express or implied
// warranty.
//
// This code accompanies the "fM2GP" book:
//
// From Mathematics to Generic Programming
// by Alexander Stepanov and Daniel E. Rose
// Addison-Wesley Professional, 2015
//
// -------------------------------------------------------------------
// ch10.cpp -- For testing functions from Chapter 10 of fM2GP.
// -------------------------------------------------------------------
// Code corrected and test case changed December 2019.
#include <iostream>
#include <vector>
#include <list>
#include "ch10.h"
struct {
bool operator()(int x) { return x == 3; }
} equal_3;
struct {
bool operator()(int x) { return x < 3; }
} less_3;
int main() {
std::vector<int> v { 1, 2, 3, 4, 5};
std::list<int> l { 1, 2, 3, 4, 5};
std::cout << "Initial sequence: 1 2 3 4 5" << std::endl;
std::cout << "vector v and list l initialized to this sequence." << std::endl;
auto x1 = begin(v);
auto y1 = end(v);
auto x2 = begin(l);
auto y2 = end(l);
std::cout << "Setting x1 = begin(v); y1 = end(v)" << std::endl;
std::cout << "Setting x2 = begin(l); y2 = end(l)" << std::endl;
std::cout << "fmgp::distance(x1, y1) is " << fmgp::distance(x1, y1) << std::endl;
std::cout << "fmgp::distance(x2, y2) is " << fmgp::distance(x2, y2) << std::endl;
fmgp::advance(x1, 3);
std::cout << "After advance(x1, 3): ";
std::cout << "(x1 == y1) is " << (x1 == y1) << std::endl;
fmgp::advance(x2, 3);
std::cout << "After advance(x2, 3): ";
std::cout << "(x2 == y2) is " << (x2 == y2) << std::endl << std::endl;
x1 = begin(v);
y1 = end(v);
x2 = begin(l);
y2 = end(l);
std::cout << "Setting x1 = begin(v); y1 = end(v)" << std::endl;
std::cout << "Setting x2 = begin(l); y2 = end(l)" << std::endl;
std::cout << "*fmgp::find_if(x1, y1, equal_3) is " << *fmgp::find_if(x1, y1, equal_3) << std::endl;
std::cout << "*fmgp::find_if(x2, y2, equal_3) is " << *fmgp::find_if(x2, y2, equal_3) << std::endl;
std::cout << "*fmgp::find_if_n(x1, 5, equal_3).first is " << *fmgp::find_if_n(x1, 5, equal_3).first << std::endl;
std::cout << "*fmgp::find_if_n(x2, 5, equal_3).first is " << *fmgp::find_if_n(x2, 5, equal_3).first << std::endl;
std::cout << "*fmgp::partition_point(x1, y1, less_3) is " << *fmgp::partition_point(x1, y1, less_3) << std::endl;
std::cout << "*fmgp::partition_point(x2, y2, less_3) is " << *fmgp::partition_point(x2, y2, less_3) << std::endl;
std::cout << "*fmgp::partition_point_n(x1, 5, less_3) is " << *fmgp::partition_point_n(x1, 5, less_3) << std::endl;
std::cout << "*fmgp::partition_point_n(x2, 5, less_3) is " << *fmgp::partition_point_n(x2, 5, less_3) << std::endl;
std::cout << "*fmgp::upper_bound(x1, y1, 2) is " << *fmgp::upper_bound(x1, y1, 2) << std::endl;
std::cout << "*fmgp::upper_bound(x2, y2, 2) is " << *fmgp::upper_bound(x2, y2, 2) << std::endl;
std::cout << "*fmgp::lower_bound(x1, y1, 2) is " << *fmgp::lower_bound(x1, y1, 2) << std::endl;
std::cout << "*fmgp::lower_bound(x2, y2, 2) is " << *fmgp::lower_bound(x2, y2, 2) << std::endl;
}