게시판 즐겨찾기
편집
드래그 앤 드롭으로
즐겨찾기 아이콘 위치 수정이 가능합니다.
주말이 끝나는 시점에서 심심해서 하나 더 sort
게시물ID : programmer_6307짧은주소 복사하기
작성자 : 황금비
추천 : 1
조회수 : 521회
댓글수 : 1개
등록시간 : 2014/11/02 17:53:00
제가 알고 있는 c의 sort, std:: sort 함수에 대해서 함수포인터, 함수자(functor), 그리고 람다표현으로 그냥 예제 함 만들어 보았습니다.


#include <algorithm>
#include <functional>
using namespace std;

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int    compareC (const char* c1, const char* c2){  return (*c1 < *c2)? 1: (*c1 > *c2) ? -1 : 0; }
int    compareS (const char& c1, const char& c2) { return (c1 < c2); }

template<class T>
struct compareF { int operator()(const T& c1, const T& c2) { return (c1 > c2); } };

int main()
{
{
char msg[] = "Hello world";
printf("\nC-sorting: Qsort-------------------------------------------------------\n");

qsort(&msg[0], strlen(msg), sizeof(char), (int (*)(const void *, const void *))compareC);
printf("%s\n", msg);
}
{
char msg[] = "welcome to todayhumor";
printf("\nstd-sorting with function ---------------------------------------------\n");

char* bgn = &msg[0];
char* end = &msg[strlen(msg)];
std::sort(bgn, end, compareS);

printf("%s\n", msg);
}
{
char msg[] = "c++ functor is difficult but very cool.";
printf("\nstd-sorting with functor ---------------------------------------------\n");

char* bgn = &msg[0];
char* end = &msg[strlen(msg)];
std::sort(bgn, end, compareF<int>());

printf("%s\n", msg);
}
{
char msg[] = "c++ goes to complete the abstraction of language as having a lambda expression.";
printf("\nstd-sorting with lambda ---------------------------------------------\n");

char* bgn = &msg[0];
char* end = &msg[strlen(msg)];
std::sort(bgn, end, [](const char& c1, const char& c2)->int{ return (c1 < c2); });

printf("%s\n", msg);
}

return 0;
}

전체 추천리스트 보기
새로운 댓글이 없습니다.
새로운 댓글 확인하기
글쓰기
◀뒤로가기
PC버전
맨위로▲
공지 운영 자료창고 청소년보호