#include <stdio.h>
#include <iostream>
#include <vector>
#include <algorithm>
#include <functional>
using namespace std;
class Test {
public:
int x, y;
Test (int x1, int y1) {
x = x1;
y = y1;
}
};
class gt1 : public binary_function<int, int, bool>
{
public:
result_type operator() (first_argument_type a, second_argument_type b)
{
return (result_type)((a > b) ? 1 : 0);
}
};
bool gt2(int a, int b)
{
return (bool)((a > b) ? 1 : 0);
};
class gt3 : public binary_function<Test, Test, bool>
{
public:
result_type operator() (first_argument_type a, second_argument_type b)
{
return (result_type)((a.y > b.y) ? 1 : 0);
}
};
bool gt4(Test a, Test b)
{
return (bool)((a.y > b.y) ? 1 : 0);
};
bool operator< (Test t1, Test t2)
{
return (t1.y > t2.y) ? true : false;
}
int main()
{
// sort,stable_sort 初期設定
vector<int> v1 {5, 10, 4, 3, 7};
// int v1[] = {5, 10, 4, 3, 7};
printf("sort,stable_sort : 初期状態 v1 :");
for (auto x : v1)
printf(" %d", x);
printf("\n");
// sort,stable_sort 昇順(小 -> 大)
printf("sort,stable_sort : 小 -> 大\n");
sort(v1.begin(), v1.end());
// sort(v1.begin(), v1.end(), less<int>()); // 二項関数オブジェクト less
// stable_sort(v1.begin(), v1.end());
// sort(v1, v1+5); // 配列の場合
printf(" v1 :");
for (auto x : v1)
printf(" %d", x);
printf("\n");
// 降順(大 -> 小)
printf("sort : 大 -> 小\n");
sort(v1.begin(), v1.end(), greater<int>()); // 二項関数オブジェクト greater
// sort(v1.begin(), v1.end(), gt1()); // 二項関数オブジェクト 新規作成
// sort(v1.begin(), v1.end(), gt2); // 関数
// sort(v1.begin(), v1.end(), [](int x, int y){ return x > y; }); // ラムダ式
// sort(v1, v1+5, greater<int>()); // 配列の場合
printf(" v1 :");
for (auto x : v1)
printf(" %d", x);
printf("\n");
// partial_sort(先頭 2 個を並べる)
vector<int> v2 {5, 2, 4, 3, 7};
printf("partial_sort : 初期状態 v2 :");
for (auto x : v2)
printf(" %d", x);
printf("\n");
partial_sort(v2.begin(), v2.begin()+2, v2.end());
printf(" v2(先頭 2 個をsort) :");
for (auto x : v2)
printf(" %d", x);
printf("\n");
// is_sorted,is_sorted_until
printf("is_sorted,is_sorted_until\n");
bool bl = is_sorted(v2.begin(), v2.begin()+3);
cout << boolalpha;
cout << " v2 の最初の 3 個は sort 済みか? : " << bl << endl;
vector<int>::iterator it = is_sorted_until(v2.begin(), v2.end());
cout << " v2 の sort されていない箇所 : ";
for (auto it1 = it; it1 != v2.end(); it1++)
cout << " " << *it1;
cout << endl;
// partial_sort_copy(v3 の 最初の 3 個を v4 にコピー)
vector<int> v3 {5, 10, 4, 3, 7};
printf("partial_sort_copy : 初期状態 v3 :");
for (auto x : v3)
printf(" %d", x);
printf("\n");
vector<int> v4(3);
partial_sort_copy(v3.begin(), v3.end(), v4.begin(), v4.end());
printf(" v3 の 最初の 3 個を v4 にコピー,v4 :");
for (auto x : v4)
printf(" %d", x);
printf("\n");
// nth_element(5 より小さい値を前に)
vector<int> v5 {10, 5, 10, 4, 3, 7};
printf("nth_element : 初期状態 v5 :");
for (auto x : v5)
printf(" %d", x);
printf("\n");
nth_element(v5.begin(), v5.begin()+1, v5.end()); // 5 ( 2 番目の値)
printf(" 処理後 v5 :");
for (auto x : v5)
printf(" %d", x);
printf("\n");
// クラスのオブジェクトを降順(大 -> 小)
Test t1(10, 30), t2(40, 10), t3(90, 50);
vector<Test> tv {t1, t2, t3};
printf("オブジェクトの sort : 初期状態 tv :");
for (auto t : tv)
printf(" (%d, %d)", t.x, t.y);
printf("\n");
printf("オブジェクトの sort : メンバー y:大 -> 小\n");
sort(tv.begin(), tv.end(), gt3()); // 二項関数オブジェクト 新規作成
// sort(tv.begin(), tv.end(), gt4); // 関数
// sort(tv.begin(), tv.end()); // 演算子 < のオーバーロード(多重定義)
printf(" tv :");
for (auto t : tv)
printf(" (%d, %d)", t.x, t.y);
printf("\n");
return 0;
}