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
|
#include <iostream>
#include <algorithm>
#include <climits>
using namespace std;
struct Foo {
Foo(int val) { this->val = val; }
Foo() { this->val = INT_MIN; }
bool operator<(const Foo& right) {
cout << "Member Overload Used\n";
return this->val < this->val;
}
friend bool operator<(Foo& left, Foo& right);
private:
int val;
};
bool operator<(Foo& left, Foo& right) {
cout << "Friend Overload Used\n";
return left.val < right.val;
}
int main() {
cout << "Test 1: " << (Foo(-1) < Foo(3)) << endl; // Member Overload Used
vector<Foo> v1 = {Foo(-1), Foo(1), Foo(2)};
bool isv1Sorted = is_sorted(begin(v1), end(v1));
cout << "is_sorted v1? " << isv1Sorted << endl; // As expected.
vector<Foo> v2 = {Foo(2), Foo(1), Foo(-1), Foo(-5), Foo(10)}; // What if it's unsorted?
bool isv2Sorted = is_sorted(begin(v2), end(v2));
cout << "is_sorted v2? " << isv2Sorted << endl; // As expected.
vector<Foo> v3 = {Foo(-5), Foo(-1), Foo(1), Foo(2), Foo(10)}; // How many calls are used when it's sorted
bool isv3Sorted = is_sorted(begin(v2), end(v2)); // Apparently still just 1.
cout << "is_sorted v3? " << isv3Sorted << endl;
return 0;
}
|