C++ delete
·
C++/Syntax
delete 함수를 사용하지 못하도록 막는 역할을 하는 키워드이다. #include #include class Fraction { int numerator_; int denominator_; public: Fraction(char) = delete; explicit Fraction(int num = 0, int den = 1) : numerator_(num), denominator_(den) { std::cout
C++ 깊은 복사(Deep Copy)
·
C++/Syntax
깊은 복사(Deep Copy) 하위 항목을 모두 복사하는 것 #include #include class MyString { char* data_ = nullptr; int len_ = 0; public: MyString(const char* source = "") { assert(source); len_ = std::strlen(source) + 1; data_ = new char[len_]; for (int i = 0; i < len_; ++i) data_[i] = source[i]; data_[len_ - 1] = &#39;\0&#39;; } ~MyString() { delete[] data_; } MyString(const MyString& source) { std::cout
C++ 얕은 복사(Shallow Copy)
·
C++/Syntax
얕은 복사(Shallow Copy) 얕은 복사는 허상 포인터(Dangling pointer) 문제를 일으킬 수 있다. #include #include class MyString { char* data_ = nullptr; int len_ = 0; public: MyString(const char* source = "") { assert(source); len_ = std::strlen(source) + 1; data_ = new char[len_]; for (int i = 0; i < len_; ++i) data_[i] = source[i]; data_[len_ - 1] = &#39;\0&#39;; } ~MyString() { delete[] data_; } char*& getString() { retu..
라즈베리파이 부팅 시 자동 실행
·
Raspberrypi
라즈베리파이 부팅 시 자동 실행 rc.local, .bashrc, init.d, systemd 등의 방법이 있지만 crontab이 가장 간편하다. crontab 자동 실행하고 싶은 명령어를 정리해서 쉘 스크립트로 작성한다. 다음 명령어를 입력한다. sudo crontab -e 나는 처음 입력하니까 에디터를 선택하라고 했는데, vim basic이 무난할 것 같아서 선택했다. crontab 설정 파일이 에디터로 열렸으면 맨 밑에 다음과 같이 실행하고 싶은 파일을 입력한다. 파일명은 절대경로로 입력해야하는 듯 하다. ... @reboot sudo 파일명.sh 리부팅하면 적용된다. 참고 https://blog.naver.com/emperonics/221770579539
C++ 복사 생성자(Copy Constructor)
·
C++/Class
복사 생성자(Copy Constructor) 보안이 중요한 경우 복사 생성자를 private로 정의하여 복사를 방지하기도 한다. 기본 복사 생성자는 얕은 복사이므로 주의하자. 이를 방지하기 위해 깊은 복사를 구현해야 하는데, 시간적 여유가 없을 경우 임시로 delete를 통해 구현하기도 한다. copy initialization, direct initialization, uniform initialization 모두 복사 생성자를 호출한다. #include #include class Fraction { int numerator_; int denominator_; public: Fraction(int num = 0, int den = 1) : numerator_(num), denominator_(den) ..
C++ 위임 생성자 (Delegating Constructor)
·
C++/Class
위임 생성자 (Delegating Constructor) C++11 생성자에서 직접 생성하지 않고, 이미 구현된 생성자를 호출해서 대신 생성하도록 하는 것이다. 기본 예제 다음 예제에서는 st2를 생성할 때 id_, name_ 멤버 변수를 초기화 리스트로 초기화하고있다. #include using namespace std; class Student { int id_; string name_; public: Student(const string& name_in) : id_{0}, name_{name_in} { print(); } Student(const int& id_in, const string& name_in) : id_{id_in}, name_{name_in} { print(); } void prin..
C++ 멤버 초기화 리스트 (Member Initializer Lists)
·
C++/Class
멤버 초기화 리스트 (Member Initializer Lists) 생성자를 만들 때 멤버들을 초기화해주는 기능 C++11부터 배열도 이 방식으로 초기화 가능하다고 한다. #include class Something { int i_; double d_; char c_; public: Something() : i_{ 1 }, d_{ 3.14 }, c_{ &#39;a&#39; } {} }; int main() { Something a; } 기본 값 적용 우선순위가 헷갈릴 때 #include class Something { int i_ = 100; double d_ = 100.0; char c_ = &#39;F&#39;; public: Something() : i_{ 1 }, d_{ 3.14 }, c_{ &#..
C++ 생성자 (Constructor)
·
C++/Class
생성자 (Constructor) 인스턴스가 처음 생성될 때 호출되는 함수 멤버 변수부터 생성하고 그 다음 호출된다. 멤버 중에 클래스가 있을 경우 해당 클래스의 생성자부터 호출된다. #include class Second { public: Second() { std::cout
C++ 접근 지정자 (Access Specifier)
·
C++/Class
접근 지정자 (Access Specifier) 종류 public : 어디서든 접근 가능하다. private : 해당 클래스와 friend 클래스만 접근 가능하다. protected : private 특성을 기본으로, 상속받은 자식 클래스도 접근 가능하다. 명시하지 않는 경우, 기본 값은 private이다. private이어도 같은 클래스면 다른 인스턴스의 멤버를 사용할 수 있다. class Date { int year_; int month_; int day_; void copyFrom(const Date& original) { year_ = original.year_; month_ = original.month_; day_ = original.day_; } }; public 멤버 변수들에 대해 unifo..
C++ Class
·
C++/Class
Class struct vs class c++에서의 구조체도 멤버 함수를 가질 수 있다. _단순한 기능_의 경우 struct를 사용해도 무방하다. class를 사용하는 이유는 다양한 객체지향 프로그래밍 기법들을 적용하기 위함이다. Access Specifier (public, private, protected 등) 접근 지정자 (Access Specifier) 생성자 (Constructor) 소멸자 (Destructor) this const static friend 익명 객체 (Anonymous Class) Nested Types 실행 시간 측정 (Run Time Measurement) 참고 따라하며 배우는 C++
따라하며 배우는 C++ 8장
·
C++/TBC++
따라하며 배우는 C++ 8장 Class 참고 따라하며 배우는 C++
C++ assert
·
C++/Library
assert 라이브러리 assert Debug 모드에서만 런타임에 작동한다. VS의 전처리기 설정에 매크로가 설정되어있다. Debug 모드에서는 _DEBUG Release 모드에서는 NDEBUG 내부 조건이 거짓이면 Debug Error를 발생시킨다. #include int main() { assert(false); } 최대한 나눠서 쓰는게 디버깅하기에 좋다. static assert 컴파일 타임에 작동한다. Release 모드에서도 작동한다. 에러 문구를 넣어야 한다. #include int main() { const int x = 5; //const int x = 4; // 컴파일 안됨 static_assert(x == 5, "x should be 5"); } 릴리즈 모드에선 작동되지 않는다면, 차라..
C++ vector
·
C++/Library
vector 라이브러리 기본 예제 push_back 함수로 벡터의 맨 뒤에 원소를 추가할 수 있다. foreach로 반복문을 작성할 수 있다. (iterator가 존재하기 때문에 사용 가능) #include #include int main() { using namespace std; vector vec; for (int i = 0; i < 10; ++i) vec.push_back(i); for (auto& e : vec) cout
C++ 함수 포인터 (Function Pointer)
·
C++/Syntax
함수 포인터 (Function Pointer) 기본 예제 배열에서 짝수, 홀수를 각각 출력하는 예제 간단한 버전 #include #include using namespace std; void printNumbers(const array& arr, \ bool print_even) { for (auto e : arr) if ((print_even && e % 2 == 0) || \ (!print_even && e % 2 == 1)) cout
C++ 인라인 함수 (Inline Function)
·
C++/Syntax
인라인 함수 (Inline Function) 함수 반환 값 정의 앞에 inline 키워드를 붙여주면 된다. 컴파일러가 인라인으로 넣을지 결정한다. inline 키워드를 넣지 않아도 인라인 함수로 작동시킬 때도 있고, 키워드를 넣어도 인라인 함수로 작동하지 않는 경우가 있다.
C++ tuple
·
C++/Library
tuple C++11 라이브러리 #include #include std::tuple getTuple() { return (std::make_tuple(10, 3.14)); } int main() { using namespace std; tuple my_tp = getTuple(); cout
따라하며 배우는 C++ 7장
·
C++/TBC++
따라하며 배우는 C++ 7장 Call by Reference std::tuple 인라인 함수 (Inline Function) 함수 포인터 (Function Pointer) std::vector assert 참고 따라하며 배우는 C++
C++ 레퍼런스 (Reference, 참조)
·
C++/Syntax
레퍼런스 (Reference, 참조) 함수의 인자로 const int & 형태를 자주 사용하는 이유 레퍼런스 : 불필요한 복사가 발생하지 않아서 성능 상 이점을 가진다. const : rvalue도 인자로 넘길 수 있어서 확장성이 좋아진다. Call by Reference 포인터를 레퍼런스로 전달하는 방법 #include void foo(int*& ptr) { std::cout
C++ Stack Size
·
C++/Syntax
Stack Size VS에서는 아래와 같은 경우 경고를 띄운다. int main() { using namespace std; int arr[10000]; (void)arr; } 경고 내용 Warning C6262 Function uses &#39;40000&#39; bytes of stack: exceeds /analyze:stacksize &#39;16384&#39;. Consider moving some data to heap. 찾아보니 VS의 기본 스택 프레임 사이즈가 16KB로 설정되어있었고, 변경 가능하다. 스택 사이즈를 제한하는 이유는 쓰레드 개수의 확보를 위해서, 또 스택 오버플로우를 방지하기 위해서라고 한다. OS에 따라 스택 오버플로우 발생 시 자동으로 스택 사이즈를 늘리는 방식도 있다고..
C++ 문자열 (string)
·
C++/Syntax
문자열 (string) text segment #include int main() { using namespace std; const char* name = "Jack jack"; const char* name2 = "Jack jack"; cout
C++ nullptr_t
·
C++/Syntax
nullptr_t C++11 C언어에서 사용되지는 않으나, 라이브러리에 존재하는 자료형이다. #include #include void f(int*) { std::cout
C++ array
·
C++/Library
std::array C++11 size(), begin(), end(), at() 등의 함수 사용 가능 at()으로 접근하는 것은 arr[1] 처럼 직접 주소로 접근하는 것보다 느리지만 조금 더 안전하다. at() 함수는 std::exception을 통해 예외 처리가 되어있다. algorithm 라이브러리의 sort 함수 사용 가능
C++ typeinfo
·
C++/Library
데이터 타입 확인 라이브러리의 typeid().name()을 사용한다. cout
C++ 배열 (Array)
·
C++/Syntax
배열 (Array) 상수 포인터, 포인터 상수는 무슨 의미인지는 알겠는데 사용하면 더 헷갈린다... 변수란 뭘까 #include int main() { using namespace std; int a = 1; cout
따라하며 배우는 C++ 6장
·
C++/TBC++
배열 (Array) nullptr_t 문자열 (string) Stack Size 레퍼런스 (Reference, 참조) float vs double const 값 변경 참고 따라하며 배우는 C++
C++ cin.ignore
·
C++/Library
입력 버퍼 무시하기 cin.ignore(_Count, _Metadelim) _Count : 무시할 문자의 최대 개수(바이트) 기본 값은 1 정석대로라면 라이브러리의 std::numeric_limits::max()를 사용하는게 맞으나... 귀찮으므로 보통 적당히 큰 수를 채택하는 것 같다. _Metadelim : 이 문자가 나올 때까지 무시한다.(해당 문자 포함) 기본 값은 eof #include int getInt() { while (true) { int x; std::cout > x; if (std::cin.fail()) { std::cin.clear(); std::cin.ignore(std::numeric_limits::max(), &#39;\n&#39;); std::cout op; std::cin...
C++ cin
·
C++/Library
cin 현재 내가 사용하는 윈도우 컴퓨터 기준으로 작성했다. 상속 관계 cin 은 istream 라이브러리 내부 basic_istream 클래스의 객체이다. basic_istream 클래스는 ios 라이브러리 내부 basic_ios 클래스를 상속받는다. basic_ios 클래스는 xiosbase 라이브러리 내부 ios_base 클래스를 상속받는다. ios_base 클래스는 _Iosb 클래스를 상속받는다. #define _CRTIMP2_IMPORT __declspec(dllimport) #define _CRTDATA2_IMPORT _CRTIMP2_IMPORT class _CRTIMP2_PURE_IMPORT ios_base : public _Iosb; class basic_ios : public ios_ba..
C++ 난수 생성 (Random Number Generation)
·
C++/Library
난수 생성 (Random Number Generation) Linear congruential generator 선형 합동 생성기 널리 알려진 유사난수 생성기이다. unsigned int PRNG() // Pseudo Random Number Generator { static unsigned int seed = 5523; // seed number seed = 8253729 * seed + 2396403; return (seed % 32768); } std::rand std::rand() 함수로 나올 수 있는 최댓값인 RAND_MAX를 이용하여 범위를 제한한다. 고르게 분포되지는 않는다. #include int getRandomNumber(int min, int max) { static const dou..
C++ 반복문
·
C++/Syntax
반복문 (Loop) unsigned int 의 연산이 일반적으로 int 보다 빠르다. 음수를 사용하는게 아니라면 unsigned int 자료형을 사용하는게 성능면에서 더 좋다.
C++ switch
·
C++/Syntax
switch switch문 내부에서 중괄호 없이 변수 선언은 가능하지만, 초기화는 안된다. 중괄호가 없으면 변수의 scope는 switch문의 중괄호까지인데, 서로 다른 case에서 변수에 접근은 가능하지만 초기화가 안된 상태로 접근할 수 있기 때문이다. 특별한 상황이 아니라면 이렇게 사용하는 것은 별로 추천하는 방법은 아니다. MS 에러 코드 #include int main() { using namespace std; int x; cin >> x; switch (x) { int a; // 이건 가능 int b = 5; // Error: initialization of &#39;b&#39; is skipped by &#39;case&#39; label case 0: int c; // 이것도 가능 int ..