#include <stdio.h>
#include <iostream>
#include <algorithm>
#include <functional>
using namespace std;
class t_A{
public:
int a;
int b;
};
#define MAX 30
t_A g_AA[3][MAX];
bool comp_A(const t_A& a, const t_A& b) {
return a.b < b.b;
}
void test_A_sort()
{
t_A* g_A = g_AA[0];
//init
for (int i = 0; i < MAX; i++) {
g_A[i].a = i;
g_A[i].b = i%7;
}
sort(g_A, g_A + MAX, comp_A);
for (int i = 0; i < MAX; i++) {
printf("%d %d\n", g_A[i].a, g_A[i].b);
}
}
////////////////////////
int g_N[MAX];
int* g_pN[MAX];
bool cmp_pN(int*a, int* b) {
return *a < *b;
}
void pointer_test() {
printf("== pointer_test ==\n");
for (int i = 0; i < MAX; i++) {
g_N[i] = i%7;
g_pN[i] = &(g_N[i]);
}
for (int i = 0; i < MAX; i++) {
printf("%d, ", *(g_pN[i]));
}
printf("\n");
sort(g_pN, g_pN + MAX, cmp_pN);
for (int i = 0; i < MAX; i++) {
printf("%d, ", *(g_pN[i]));
}
printf("\n");
}
////////////////////////
int g_NN[3][MAX];
int* g_pNN[3][MAX];
bool cmp_pNN(int*a, int* b) {
return *a < *b;
}
void pointer_test_2() {
printf("== pointer_test_2 ==\n");
for (int j = 0; j < 3; j++) {
for (int i = 0; i < MAX; i++) {
g_NN[j][i] = i % 9 + j;
g_pNN[j][i] = &(g_NN[j][i]);
}
}
for (int i = 0; i < MAX; i++) {
printf("%d, ", *(g_pNN[0][i]));
}
printf("\n");
sort(g_pNN[0], g_pNN[0] + MAX, cmp_pNN);
for (int i = 0; i < MAX; i++) {
printf("%d, ", *(g_pNN[0][i]));
}
printf("\n");
}
////////////////////////
class t_AAA {
public:
int a;
int b;
};
t_AAA g_AAA[3][MAX];
t_AAA* g_pAAA[3][MAX];
bool cmp_pAAA(t_AAA* l, t_AAA* r) {
return l->b < r->b;
}
void pointer_test_3() {
printf("== pointer_test_3 ==\n");
for (int j = 0; j < 3; j++) {
for (int i = 0; i < MAX; i++) {
g_AAA[j][i].a = i;
g_AAA[j][i].b = i % 9 + j;
g_pAAA[j][i] = &(g_AAA[j][i]);
}
}
for (int i = 0; i < MAX; i++) {
printf("(%2d, %2d), ", (g_pAAA[0][i]->a), (g_pAAA[0][i]->b));
}
printf("\n");
sort(g_pAAA[0], g_pAAA[0] + MAX, cmp_pAAA);
for (int i = 0; i < MAX; i++) {
printf("(%2d, %2d), ", (g_pAAA[0][i]->a), (g_pAAA[0][i]->b));
}
printf("\n");
}
////////////////////////
bool cmp(int a, int b) { return a<b; }
int main()
{
int a[] = { 6, 1, 3, 2, 4 };
sort(a, a + 5, greater<int>());
sort(a, a + 5, less<int>());
sort(a, a + 5, cmp);
for (int i = 0; i < (sizeof(a) / sizeof(int)); i++) printf("%d, ", a[i]);
printf("\n");
//test_A_sort();
//pointer_test();
pointer_test_3();
return 0;
}
'Programming > C Programming' 카테고리의 다른 글
cpp 의 sort 사용하기 예제 (0) | 2018.03.16 |
---|---|
strcmp 함수의 모호한 return value (0) | 2017.02.20 |
이차원 배열(포인터) 활용 (0) | 2016.02.24 |
화면 출력을 버퍼로 변경하기 (0) | 2016.02.18 |
stdout 출력을 file 로 돌리기 (0) | 2016.02.04 |