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
|
// forward_list::unique
#include <iostream>
#include <cmath>
#include <forward_list>
// a binary predicate implemented as a function:
bool same_integral_part (double first, double second)
{ return ( int(first)==int(second) ); }
// a binary predicate implemented as a class:
class is_near_class
{
public:
bool operator() (double first, double second)
{ return (fabs(first-second)<5.0); }
} is_near_object;
int main ()
{
std::forward_list<double> mylist = { 15.2, 73.0, 3.14, 15.85, 69.5,
73.0, 3.99, 15.2, 69.2, 18.5 };
mylist.sort(); // 3.14, 3.99, 15.2, 15.2, 15.85
// 18.5, 69.2, 69.5, 73.0, 73.0
mylist.unique(); // 3.14, 3.99, 15.2, 15.85
// 18.5, 69.2, 69.5, 73.0
mylist.unique (same_integral_part); // 3.14, 15.2, 18.5, 69.2, 73.0
mylist.unique (is_near_object); // 3.14, 15.2, 69.2
std::cout << "mylist contains:";
for (double& x: mylist) std::cout << ' ' << x;
std::cout << '\n';
return 0;
}
| |