본문 바로가기

C++

(25)
C++) std::bind 와 std::placeholders (std::placeholders::_1, std::placeholders::_2, ...) std::bind std::bind를 알려면 std::function을 알아야 해요 std::function C+11에서 나온 함수 포인터로 사용 할 수 있는 std::function std::function name = &함수 이런 식으로 선언을 해요 예) std::function myFunc = &calculate; 이때 calculate는 이미 bool calculate(int, int) 라는 형식을 갖고 선언이 되있어요. 그러면 다시 std::bind를 볼께요 std::bind std::bind는 반환값이 std::function 인데요 특정 인자를 갖거나 특정 객체의 함수를 부르도록 구체적인 std::function을 제공해요 위의 예) std::function myFunc = &calculat..
C++) RAII 디자인 패턴 RAII: Resource Acquisition Is Initialization. 리소스를 초기화 할 때 메모리 할당까지 이루어지는 방식이다. RAII를 기반으로 만들어진게 shared_ptr, unique_ptr이 있다. string을 예로 들어 설명 하겠다. class String { public: String() { new char[256]; } ~String() { delete[] char; } } 위 클래스 String은 RAII를 따르도록 구현되었다. String의 생성자에서 메모리를 할당하고, String의 소멸자에서 메모리를 제거한다. 이렇게 하므로서 얻는 장점은 다음과 같다. int main() { char* str = new char[256]; ... // 함수 실행 도중 error 발..
(C언어) epoll 정의 epoll 리눅스에서 select 대신 사용 가능한 I/O subscription 모델 FD를 등록하여 해당 FD를 계속 확인하거나 일정 시간 확인하여 요청된 이벤트를 처리하는 메케니즘 int epoll_create(int size); fd들의 입출력 이벤트 저장을 위한 공간 할당 int epoll_ctl(int epfd, int op, int fd, struct epoll_event *event) op 값으로 EPOLL_CTL_ADD, EPOLL_CTL_MOD, EPOLL_CTL_DEL 이 들어 올 수 있음 EPOLL_CTL_ADD: epoll에 fd를 등록 (fd로 처리 할 이벤트 종류 등록) [종류: EPOLLIN, EPOLLOUT, EPOLLPRI 등] int epoll_wait(int epfd,..
[프로그래머스] (C++) 점프와 순간이동 문제 문제는 이 곳에 가시면 있습니다. 힌트 일부의 최적의 해를 사용해 전체의 최적의 해를 구한다. 소스 코드 (1차 시도) //[프로그래머스] //점프와 순간 이동 #include #include #include using namespace std; vector gMin; int getMin(int n) { if (gMin[n] != 0) return gMin[n]; if (n % 2 == 0) { if (getMin(n/2) < (getMin(n-1) + 1)) { gMin[n] = getMin(n/2); } else { gMin[n] = getMin(n-1) + 1; } } else { gMin[n] = getMin(n-1) + 1; } return gMin[n]; } int solution(int..
[프로그래머스] (C++) 최댓값과 최솟값 문제 소개 문제는 이 링크를 따라가시면 있습니다. 소스 #include #include using namespace std; string solution(string s) { string answer = ""; istringstream iss{s}; string numStr; int num; vector intVec; while (std::getline(iss, numStr, ' ')) { num = std::stoi(numStr); intVec.push_back(num); } int max = *std::max_element(intVec.begin(), intVec.end()); int min = *std::min_element(intVec.begin(), intVec.end()); answer = s..
[프로그래머스] (C++) 단체사진 찍기 예제 테스트 방법 input.txt 2 2 [N~F=0, R~T>2] 2 [M~C\1] main.cc int main () { int T; int n; std::string line; std::freopen("input.txt", "r", stdin); std::cin >> T; std::vector data; for (int i = 0; i > n; for (int j = 0; j > line; if (j == 0) { line.erase(0, 1); } line.erase(line.end()-1); data.push_back(line); } std::cout
[C++] std::max, std::max_element 또는 std::sort 에서 compare 함수의 역할 template T max( std::initializer_list ilist, Compare comp ); std::initializer_list 관련해서는 다른 문서를 읽어보면 좋아요. template ForwardIt max_element( ForwardIt first, ForwardIt last, Compare comp ); ForwardIt는 한쪽 방향으로만 전진하는 Iterator를 뜻합니다. template void sort( RandomIt first, RandomIt last, Compare comp ); RandomIt는 ..
[C++] 클래스 멤버 함수 (class member function) 를 static (정적) 으로 선언 하는 이유 본론으로 가기 전에 정적 멤버 변수 (class member variable) 부터 보도록 하겠습니다. 정적 멤버 변수는 전역 변수와 저장원리가 같습니다. 정적 멤버 변수는 클래스 외부 (file scope) 단위로 초기화 되어야 합니다. 글로벌 변수를 초기화 하는 것처럼. 소스 코드 파일 (.cc) 내 함수 외부에서 int class::variable = 0 이런식으로 초기화 되어야 합니다. private로 선언한 정적 멤버 변수 역시 같은 방법으로 초기화 되어야 합니다. private 클래스 멤버의 경우 클래스 함수 내에서만 접근 가능하지만 이같이 소스 파일에 적어 초기화를 할 때만 예외를 둡니다. 정적 멤버 변수는 클래스 내 모든 객체가 공통으로 사용 할 수 있습니다. public으로 선언된 정적 ..