본문 바로가기
728x90
cpp 멤버 이니셜라이저(Member Initializer) cpp에서는 멤버 이니셜라이저를 지원! 멤버 이니셜라이저가 뭘까? 멤버(member) = 클래스 내의 멤버 변수 이니셜라이저(initializer) = 초기화 멤버 이니셜라이저 = 클래스가 생성되면서 바로 멤버 변수를 초기화해주는 것 멤버 이니셜라이저가 왜 생겼을까!? 멤버 이니셜라이저가 있기 전에는 밑에 코드와 같이 생성자 안에서 변수를 초기화해줘야 했다 class A { private: int a; public: A(int _a) { a = _a; } } 이렇게 생성자를 통해서 멤버 변수를 초기화하는 것은 불편하기에 멤버 이니셜라이저(Member Initializer) 필요! 멤버 이니셜라이저의 장점 1. 초기화의 대상을 명확히 인식이 가능 2. 선언과 동시에 초기화가 이뤄지는 바이너리 코드가 생성되.. 2022. 1. 3.
cpp static const 변수 c++98으로만 코드를 짜는 중에 발생한 오류! error: default member initializer for non-static data member is a C++11 extension [-Werror,-Wc++11-extensions] 클래스 안에서 변수 초기화는 c++11부터 지원해준다!😱 class A { private: int a = 0; } 이렇게 쓰는 것이 불가능!! c++98에서 static const 변수에는 클래스 안에서 초기화가 가능! 그렇다면 static const는 왜 클래스 안에서 초기화가 가능할까!? static 변수란? 객체들이 생성될 때마다 생성되는 것이 아닌, 클래스가 정의되기 이전(프로그램 시작과 끝까지)부터 메모리에 할당된 변수 = 클래스 변수(클래스 당 하나씩.. 2022. 1. 3.
티스토리 블로그 검색 설정 보호되어 있는 글 입니다. 2022. 1. 3.
leetcode 878) Nth Magical Number LV. Hard 🥵 https://leetcode.com/problems/nth-magical-number/ Nth Magical Number - LeetCode Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview. leetcode.com 문제 A positive integer is magical if it is divisible by either a or b. Given the three integers n, a, and b, return the nth magical number. Since the ans.. 2022. 1. 3.
문제에서 왜 항상 1e9+7로 나눈 나머지를 구하라고 할까?(모듈러 연산) 알고리즘을 풀다가 정답이 너무 클 수도 있으니 1000000007(1e9+7)로 나눈 나머지를 return 하라고 나왔다 왜 굳이 1e9+7를 return 하라는 걸까? C/C++에서 수의 표현은 제한적이다 int(4 bytes) : -2147483648(-2^31) ~ 2147483647(2^31-1) -> 근삿값 : 2e9 long long(8 bytes) : -9.22337204e18(-2^63) ~ 9.22337204e18(2^63) -> 근삿값 : 9e18 21! 만 해도 long long 범위를 벗어남 -> 조합이나 가짓수를 세는 문제에서 쉽게 오버플로우가 난다! 큰 결과를 표현하는 데에는 많은 방법이 있지만, 굳이 변환을..? 문제 출제자는 큰 수를 표현하는 방법을 원하는 게 아니라 답을 도.. 2022. 1. 2.
leetcode 790) Domino and Tromino Tiling LV. Medium 🧐 https://leetcode.com/problems/domino-and-tromino-tiling/ Domino and Tromino Tiling - LeetCode Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview. leetcode.com 문제 You have two types of tiles: a 2 x 1 domino shape and a tromino shape. You may rotate these shapes. Given an integer n, return the nu.. 2022. 1. 2.
leetcode 1217) Minimum Cost to Move Chips to The Same Position LV. Easy 😎 https://leetcode.com/problems/minimum-cost-to-move-chips-to-the-same-position/ Minimum Cost to Move Chips to The Same Position - LeetCode Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview. leetcode.com 문제 We have n chips, where the position of the ith chip is position[i]. We need to move all the.. 2022. 1. 2.
사이트 모음 보호되어 있는 글 입니다. 2021. 12. 30.
cpp 멤버 함수 안에서 자신과 동일한 클래스 객체 private 멤버 접근 밑에 코드를 보면 원래 temp.num은 private이라 바로 접근해서 값을 바꿀 수 없다 근데 저 코드를 컴파일해보면 오류가 안 난다!! 왜일까!?? class Sum { private: int num; public: void sum { // num이 private이지만 자신의 클래스와 동일한 멤버 함수에서는 num에 접근 가능!! Sum temp; temp.num = 3; } } 멤버 함수 안에서 자신과 동일한 클래스 객체를 생성했을 때, 그 클래스 객체는 자기 클래스의 멤버 함수에 있는 것처럼 자신의 private 멤버에 자유롭게 접근이 가능하다! 너무 신기함!! 🤭🤭 2021. 12. 30.
cpp 증감 연산자 오버로딩 (전위 연산자, 후위 연산자) 전위 연산자와 후위 연산자가 똑같이 "operator++() {}" "operator--() {}" 형태인데 어떻게 두 개를 구분할까?? 전위 연산자 후위 연산자 파라미터 void int (int 값의 의미는 없고 단지 전위, 후위를 구분하기 위함) 리턴값 객체 자체 임시 객체 속도 빠름 느림 (객체의 새로 만들어 return하기에 속도가 느림) | ✍️ 전위 연산자 예시 // void값을 받으면 전위 연산자임. Fixed& Fixed::operator++(void) { this->setRawBits(this->getRawBits() + 1); return *this; } Fixed& Fixed::operator--(void) { this->setRawBits(this->getRawBits() - 1);.. 2021. 12. 30.
728x90