본문 바로가기

Programming/C&C++&C#

(17)
modern C++ 스마트포인터 unique_ptr Smart Pointer의 unique_ptr 사용하기 포인터 대입 복사 안됨. 소유권 이전시 move() 사용 unique_ptr로 할당한 메모리는 reset()으로 초기화, delete로 초기화시 에러 #include #include using namespace std; class Point { int x, y; public: Point(int _x, int _y) : x(_x), y(_y) { cout
modern C++ begin/end begin(), end() 함수 사용 #include #include #include #include using namespace std; template void show (T& c) { auto p1 = begin(c); auto p2 = end(c); while (p1 != p2) { cout
modern C++ noexcept c++17 #include using namespace std; int func1() noexcept // { throw 1; // error, call terminate() } int func2() noexcept(false) { throw 1; } int main() { try { // func1(); // terminate() func2(); } catch(...) { cout
modern C++ using 사용법 #include using namespace std; // 1. type alias using DWORD = int ; using FP = void(*)(int); // 2. template alias template class Point { T x, y; }; template using PT = Point; // 3. 템플릿 인자 고정 template using Duo = pair; template using I_Duo = pair; // 4. 복잡한 문법 단순화 template using remove_pointer_t = typename remove_pointer::type ; int main (void) { DWORD n ; // int n FP p ; // void(*p)(int) Point p1..
modern C++ static_assert c++ 11에서 추가 컴파일 타임에서 주어진 식이 false면 메시지 출력 & 컴파일 종료 변수는 사용 못함(중간에 값 변경 가능성이 있음) #include using namespace std; int main (void) { static_assert( sizeof(void*) == 4 , "32bit"); static_assert( sizeof(void*) == 8 , "64bit"); } 빌드 환경이 32bit인지 64bit인지 확인 가능
modern C++ 11/14 iteral & type, auto & decltype #include using namespace std; int main (void) { // literal & type int n1 = 0b1001; // 2진수로 표시 int n2 = 010; // 8진수 int n3 = 10 ; // 10진수 int n4 = 0x10; // 16진수 int number = 1'001'000; // 컴파일에서 '문자가 무시된다. 단위 셀때 편함. long long n3; // 64bit 정수 변수 // auto / decltype double x[3] = {0, 1, 2}; // auto n4 = x[0]; // 우변의 수식으로 좌변 타입이 결정됨 decltype(n1) d2; // int d2와 같음 }
modern C++ 참고하기 좋은 사이트 www.isocpp.org www.cppreference.com
[C언어]매크로 (Mecro) 사용 매크로 (Mecro) 매크로 종류1. 단순 매크로 : 선언 자체의 의미를 갖는 매크로2. 함수형 매크로 : 전달 인자의 형태로 변환3. 객체형 매크로 : 치환 문자열로 단순 치환 1234#define MY_MECRO // 단순 매크로 선언#define PI (3.14) // 매크로 상수 선언#define PC "Personal Computer" // 매크로 상수 선언#define PRN(x) printf("%d\n",x) // 매크로 함수 선언cs 매크로 장점 1. 코드를 간결하게 만들어준다.2. Literal 상수 사용시 유지보수 용이3. 함수형 매크로는 실제 함수보다 처리 속도가 빠름 매크로 단점1. 매크로 함수에서 치환리스트의 모든 매개변수에 괄호를 사용해야 한다.2. 매크로 함수의 치환 리스트에도..