'c++'에 해당되는 글 10건

  1. 2023.07.12 c++ const 함수와 mutable
  2. 2023.04.12 책 : C++ 최적화 최고 성능을 구현하는 10가지 검증된 기법
  3. 2022.02.21 c++ getline 예제 코드
  4. 2021.12.02 c++ int 형 타입 정리(32bit, 64bit, MAX, MIN)
  5. 2021.12.01 std::lock_guard, std::unique_lock 에서 lock, unlock 호출 시점 확인 하기
  6. 2021.11.30 데드락이 발생하는 케이스
  7. 2021.11.30 ubuntu c++ 맨페이지 설치
  8. 2021.01.28 책 "전문가를 위한 C++" - 1.2 C++의 고급기능
  9. 2021.01.27 [링크] 연산자 우선순위
  10. 2021.01.05 책 "전문가를 위한 C++" - 1.1 C++의 기초

c++ const 함수와 mutable

프로그래밍/c,c++ 2023. 7. 12. 00:07
반응형

const 함수는 값 변경을 막는데 mutable 키워드를 사용하면 값 변경이 가능하다.

의도적으로 값 변경을 막는 함수에서 왜 mutable이 필요할까?

 

함수 매개변수로 전달되는 변수에 대한 const는 그 값 자체 변경을 막아 주어 안전한 사용이 가능 하다.

함수에서 멤버 변수 값을 변경하지 않는 것을 보장 할때 const 키워드를 함수 선언에 붙인다.

const 함수 여러 예외 상황을 발생 시킨다.

 

1. 같은 const 함수 외 함수 호출을 할수 없다.

2. 멤버 변수 값을 변경 할수 없다.(전역 변수 변경은 가능하다)

    이 경우는 대부분 원하는 상황이다.

내가 개발 중인 클래스 멤버 함수에 const를 설정했을 경우에는 이슈가 있을때 맞춰서 수정이 가능하다.

const로 설정하는게 힘들 경우 지워서라도 해결을 할수 있다.

 

문제는 수정 불가능한 라이브러리를 사용하는데 const 함수를 재정의 해야 할 경우 발생한다.

Aa 클래스가 라이브러리 클래스 일 경우 Ba는 const 함수를 정의 해야만 한다.

Ba 클래스에서 멤버 변수값 변경이 필요할 경우 const를 지울수도, const 가 없는 함수를 호출 할수도 없다.

이때 멤버 번수에 mutable를 지정하여 사용하면 멤버 변수 값 변경이 가능해진다.

일반적으로 멤버 변수 값을 바꾸고 싶지 않을때 const를 사용하기 때문에 mutable 사용이 어색한 면도 있다.

만약 printTest 함수가 멀티 스레드 환경에서 동작 하고 mutex로 싱크를 맞춰야 한다면 어떻게 해야 할까?

void std::mutex::lock(); 함수는 const 함수가 아니고, 그래서 printTest에서 사용 할수 없다.

"mutable std::mutex mMutex;" 같이 mutable 을 사용하면 const 함수에서도 lock 이 가능해 진다.

mutable 은 이런 경우 처럼 로직적으로 필요하지만 const로 사용 불가능해진 상황을 위해 필요한 기능으로 보여진다.

반응형
:

책 : C++ 최적화 최고 성능을 구현하는 10가지 검증된 기법

2023. 4. 12. 22:30
반응형

http://www.yes24.com/Product/Goods/74971458

 

C++ 최적화 - YES24

빠르고 간결한 C++을 위한 프로의 최적화 팁! 빠른 코드를 작성하는 시간은 느린 코드를 작성하는 시간보다 결코 길지 않다. 이 책은 올바른 코드를 구현하면서도 빠른 C++ 프로그램을 만드는 최

www.yes24.com

업무에 실제 사용 될 팁도 있고, 최적화시 생각해 볼만한 관점도 알려 주는 책.

읽는 대로 다 이해 되지 않는 쉽지 않은 책....

반응형
:

c++ getline 예제 코드

프로그래밍/c,c++ 2022. 2. 21. 14:01
반응형

코드

#include <iostream>

using namespace std;
int main() {
    string input = "";

    while (input != "quit") {
        getline(cin, input);
        cout << "read string = " << input << endl;
    }

    return 0;
}

 

실행

$ ./a.out

read string = 
aa
read string = aa
aa bb
read string = aa bb
quit
read string = quit
반응형
:

c++ int 형 타입 정리(32bit, 64bit, MAX, MIN)

프로그래밍/c,c++ 2021. 12. 2. 17:35
반응형

int 형 타입은 32bit, 64bit 에 따라, OS 에 따라 달라질수 있다.

가능하면 int32_t 등 bit 수를 명시하는 방법으로 코딩 하는 것도 괜찮은 방법.

32 bit linux 64 bit linux
char                          1 bytes
unsigned char                 1 bytes
-----------------------------------------------
short                         2 bytes
unsigned short                2 bytes
-----------------------------------------------
int                           4 bytes
unsigned int                  4 bytes
-----------------------------------------------
long                          4 bytes
unsigned long                 4 bytes
-----------------------------------------------
long int                      4 bytes
unsigned long int             4 bytes
-----------------------------------------------
long long                     8 bytes
unsigned long long            8 bytes
-----------------------------------------------
long long int                 8 bytes
unsigned long long int        8 bytes
-----------------------------------------------
int8_t                        1 bytes
uint8_t                       1 bytes
-----------------------------------------------
int16_t                       2 bytes
uint16_t                      2 bytes
-----------------------------------------------
int32_t                       4 bytes
uint32_t                      4 bytes
-----------------------------------------------
int64_t                       8 bytes
uint64_t                      8 bytes




CHAR_MIN       0                        
CHAR_MAX        255                       
-----------------------------------------------
UCHAR_MIN      0                        
UCHAR_MAX       255                       
-----------------------------------------------
SHRT_MIN       -32768                   
SHRT_MAX        32767                     
-----------------------------------------------
USHRT_MIN      0                        
USHRT_MAX       65535                     
-----------------------------------------------
INT_MIN        -2147483648              
INT_MAX         2147483647                
-----------------------------------------------
UINT_MIN       0                        
UINT_MAX        4294967295                
-----------------------------------------------
LONG_MIN       -2147483648              
LONG_MAX        2147483647                
-----------------------------------------------
ULONG_MIN      0                        
ULONG_MAX       4294967295                
-----------------------------------------------
LLONG_MIN      -9223372036854775808     
LLONG_MAX       9223372036854775807       
-----------------------------------------------
ULLONG_MIN     0                        
ULLONG_MAX      18446744073709551615  
char                          1 bytes
unsigned char                 1 bytes
-----------------------------------------------
short                         2 bytes
unsigned short                2 bytes
-----------------------------------------------
int                           4 bytes
unsigned int                  4 bytes
-----------------------------------------------
long                          8 bytes
unsigned long                 8 bytes
-----------------------------------------------
long int                      8 bytes
unsigned long int             8 bytes
-----------------------------------------------
long long                     8 bytes
unsigned long long            8 bytes
-----------------------------------------------
long long int                 8 bytes
unsigned long long int        8 bytes
-----------------------------------------------
int8_t                        1 bytes
uint8_t                       1 bytes
-----------------------------------------------
int16_t                       2 bytes
uint16_t                      2 bytes
-----------------------------------------------
int32_t                       4 bytes
uint32_t                      4 bytes
-----------------------------------------------
int64_t                       8 bytes
uint64_t                      8 bytes




CHAR_MIN       -128                     
CHAR_MAX        127                       
-----------------------------------------------
UCHAR_MIN      0                        
UCHAR_MAX       255                       
-----------------------------------------------
SHRT_MIN       -32768                   
SHRT_MAX        32767                     
-----------------------------------------------
USHRT_MIN      0                        
USHRT_MAX       65535                     
-----------------------------------------------
INT_MIN        -2147483648              
INT_MAX         2147483647                
-----------------------------------------------
UINT_MIN       0                        
UINT_MAX        4294967295                
-----------------------------------------------
LONG_MIN       -9223372036854775808     
LONG_MAX        9223372036854775807       
-----------------------------------------------
ULONG_MIN      0                        
ULONG_MAX       18446744073709551615      
-----------------------------------------------
LLONG_MIN      -9223372036854775808     
LLONG_MAX       9223372036854775807       
-----------------------------------------------
ULLONG_MIN     0                        
ULLONG_MAX      18446744073709551615 

테스트 코드

#include <iostream>
#include <iomanip>
#include <climits>

int main() {
    std::cout << std::left << std::setw(30) << "char" << sizeof(char) <<  " bytes" << std::endl;
    std::cout << std::left << std::setw(30) << "unsigned char" << sizeof(unsigned char) <<  " bytes" << std::endl;
    std::cout << "-----------------------------------------------" << std::endl;
    std::cout << std::left << std::setw(30) << "short" << sizeof(short) <<  " bytes" << std::endl;
    std::cout << std::left << std::setw(30) << "unsigned short" << sizeof(unsigned short) <<  " bytes" << std::endl;
    std::cout << "-----------------------------------------------" << std::endl;
    std::cout << std::left << std::setw(30) << "int" << sizeof(int) <<  " bytes" << std::endl;
    std::cout << std::left << std::setw(30) << "unsigned int" << sizeof(unsigned int) <<  " bytes" << std::endl;
    std::cout << "-----------------------------------------------" << std::endl;
    std::cout << std::left << std::setw(30) << "long" << sizeof(long) <<  " bytes" << std::endl;
    std::cout << std::left << std::setw(30) << "unsigned long" << sizeof(unsigned long) <<  " bytes" << std::endl;
    std::cout << "-----------------------------------------------" << std::endl;
    std::cout << std::left << std::setw(30) << "long int" << sizeof(long int) <<  " bytes" << std::endl;
    std::cout << std::left << std::setw(30) << "unsigned long int" << sizeof(unsigned long int) <<  " bytes" << std::endl;
    std::cout << "-----------------------------------------------" << std::endl;
    std::cout << std::left << std::setw(30) << "long long" << sizeof(long long) <<  " bytes" << std::endl;
    std::cout << std::left << std::setw(30) << "unsigned long long" << sizeof(unsigned long long) <<  " bytes" << std::endl;
    std::cout << "-----------------------------------------------" << std::endl;
    std::cout << std::left << std::setw(30) << "long long int" << sizeof(long long int) <<  " bytes" << std::endl;
    std::cout << std::left << std::setw(30) << "unsigned long long int" << sizeof(unsigned long long int) <<  " bytes" << std::endl;
    std::cout << "-----------------------------------------------" << std::endl;
    std::cout << std::left << std::setw(30) << "int8_t" << sizeof(int8_t) <<  " bytes" << std::endl;
    std::cout << std::left << std::setw(30) << "uint8_t" << sizeof(uint8_t) <<  " bytes" << std::endl;
    std::cout << "-----------------------------------------------" << std::endl;
    std::cout << std::left << std::setw(30) << "int16_t" << sizeof(int16_t) <<  " bytes" << std::endl;
    std::cout << std::left << std::setw(30) << "uint16_t" << sizeof(uint16_t) <<  " bytes" << std::endl;
    std::cout << "-----------------------------------------------" << std::endl;
    std::cout << std::left << std::setw(30) << "int32_t" << sizeof(int32_t) <<  " bytes" << std::endl;
    std::cout << std::left << std::setw(30) << "uint32_t" << sizeof(uint32_t) <<  " bytes" << std::endl;
    std::cout << "-----------------------------------------------" << std::endl;
    std::cout << std::left << std::setw(30) << "int64_t" << sizeof(int64_t) <<  " bytes" << std::endl;
    std::cout << std::left << std::setw(30) << "uint64_t" << sizeof(uint64_t) <<  " bytes" << std::endl;

    std::cout << std::endl << std::endl << std::endl << std::endl;

    std::cout << std::left << std::setw(15) << "CHAR_MIN" << std::left << std::setw(25) << CHAR_MIN << std::endl << std::left << std::setw(16) << "CHAR_MAX" << std::left << std::setw(26) << CHAR_MAX << std::endl;
    std::cout << "-----------------------------------------------" << std::endl;
    std::cout << std::left << std::setw(15) << "UCHAR_MIN" << std::left << std::setw(25) << 0 << std::endl << std::left << std::setw(16) << "UCHAR_MAX" << std::left << std::setw(26) << UCHAR_MAX << std::endl;
    std::cout << "-----------------------------------------------" << std::endl;
    std::cout << std::left << std::setw(15) << "SHRT_MIN" << std::left << std::setw(25) << SHRT_MIN << std::endl << std::left << std::setw(16) << "SHRT_MAX" << std::left << std::setw(26) << SHRT_MAX << std::endl;
    std::cout << "-----------------------------------------------" << std::endl;
    std::cout << std::left << std::setw(15) << "USHRT_MIN" << std::left << std::setw(25) << 0 << std::endl << std::left << std::setw(16) << "USHRT_MAX" << std::left << std::setw(26) << USHRT_MAX << std::endl;
    std::cout << "-----------------------------------------------" << std::endl;
    std::cout << std::left << std::setw(15) << "INT_MIN" << std::left << std::setw(25) << INT_MIN << std::endl << std::left << std::setw(16) << "INT_MAX" << std::left << std::setw(26) << INT_MAX << std::endl;
    std::cout << "-----------------------------------------------" << std::endl;
    std::cout << std::left << std::setw(15) << "UINT_MIN" << std::left << std::setw(25) << 0 << std::endl << std::left << std::setw(16) << "UINT_MAX" << std::left << std::setw(26) << UINT_MAX << std::endl;
    std::cout << "-----------------------------------------------" << std::endl;
    std::cout << std::left << std::setw(15) << "LONG_MIN" << std::left << std::setw(25) << LONG_MIN << std::endl << std::left << std::setw(16) << "LONG_MAX" << std::left << std::setw(26) << LONG_MAX << std::endl;
    std::cout << "-----------------------------------------------" << std::endl;
    std::cout << std::left << std::setw(15) << "ULONG_MIN" << std::left << std::setw(25) << 0 << std::endl << std::left << std::setw(16) << "ULONG_MAX" << std::left << std::setw(26) << ULONG_MAX << std::endl;
    std::cout << "-----------------------------------------------" << std::endl;
    std::cout << std::left << std::setw(15) << "LLONG_MIN" << std::left << std::setw(25) << LLONG_MIN << std::endl << std::left << std::setw(16) << "LLONG_MAX" << std::left << std::setw(26) << LLONG_MAX << std::endl;
    std::cout << "-----------------------------------------------" << std::endl;
    std::cout << std::left << std::setw(15) << "ULLONG_MIN" << std::left << std::setw(25) << 0 << std::endl << std::left << std::setw(16) << "ULLONG_MAX" << std::left << std::setw(26) << ULLONG_MAX << std::endl;

    return 0;
}
반응형
:

std::lock_guard, std::unique_lock 에서 lock, unlock 호출 시점 확인 하기

프로그래밍/c,c++ 2021. 12. 1. 17:50
반응형

std::lock_guard 은 생성 시점에 lock을 호출하고 소멸 시점(블럭 끝)에 unlock을 호출 한다.

 

std::unique_lock 은 생성 시점에 lock을 호출하고 소멸 시점(블럭 끝)에 unlock을 호출 하고

추가로 lock, unlock을 호출 할수 있다(lock 에 대한 상태값 유지).

 

#include <iostream>
#include <string>
#include <mutex>

class TestMutex {
    public:
        void lock() {
            std::cout << "call lock" << std::endl;
        }
        void unlock() {
            std::cout << "call unlock" << std::endl;
        }
};

TestMutex mutex1;

int main() {
    std::cout << "###### lock_guard start" << std::endl;
    {
        std::lock_guard<TestMutex> lockGuard(mutex1);
        std::cout << "lock_guard created" << std::endl;
    }
    std::cout << "###### lock_guard end" << std::endl << std::endl;

    std::cout << "###### unique_lock start" << std::endl;
    {
        std::unique_lock<TestMutex> uniqueLock(mutex1);
        std::cout << "unique_lock created" << std::endl;
    }
    std::cout << "###### unique_lock end" << std::endl << std::endl;

    std::cout << "###### unique_lock start 2" << std::endl;
    {
        std::unique_lock<TestMutex> uniqueLock(mutex1);
        std::cout << "unique_lock created" << std::endl;
        std::cout << "manual call unlock ++" << std::endl;
        uniqueLock.unlock();
        std::cout << "manual call unlock --" << std::endl;
    }
    std::cout << "###### unique_lock end 2" << std::endl << std::endl;

    std::cout << "###### unique_lock start 3" << std::endl;
    {
        std::unique_lock<TestMutex> uniqueLock(mutex1);
        std::cout << "unique_lock created" << std::endl;
        std::cout << "manual call unlock ++" << std::endl;
        uniqueLock.unlock();
        std::cout << "manual call unlock --" << std::endl;
        std::cout << "manual call lock ++" << std::endl;
        uniqueLock.lock();
        std::cout << "manual call lock --" << std::endl;
    }
    std::cout << "###### unique_lock end 3" << std::endl << std::endl;


    return 0;
}

실행 결과

$ ./a.out
###### lock_guard start
call lock
lock_guard created
call unlock
###### lock_guard end

###### unique_lock start
call lock
unique_lock created
call unlock
###### unique_lock end

###### unique_lock start 2
call lock
unique_lock created
manual call unlock ++
call unlock
manual call unlock --
###### unique_lock end 2

###### unique_lock start 3
call lock
unique_lock created
manual call unlock ++
call unlock
manual call unlock --
manual call lock ++
call lock
manual call lock --
call unlock
###### unique_lock end 3
반응형
:

데드락이 발생하는 케이스

프로그래밍/c,c++ 2021. 11. 30. 17:35
반응형

단일 스레드 데드락

#include <mutex>    
                                                                                                                                                                                                                                         
std::mutex mutex1;    
    
/////////// Lock 이 걸린 상태에서 다시 lock 호출    
void deadlock_1() {    
    std::lock_guard<std::mutex> lockGuard(mutex1);    
    deadlock_1_1();    
}                                                                                                                                                                                                                                    
     
void deadlock_1_1() {    
    std::lock_guard<std::mutex> lockGuard(mutex1); // 걸려 있는 lock에 대해 다시 lock 시도   
}    
     
////////// Lock 이 걸린 상태에서 돌고 돌아 다시 lock 호출, 많이 돌고 돌다 lock 이 걸리면 ...    
void deadlock_2() {    
    std::lock_guard<std::mutex> lockGuard(mutex1);    
    deadlock_2_1();    
}    
                                                                                     
void deadlock_2_1() {                                                                
    deadlock_2();                               // 재귀 호출로 다시 lock 시도                                                                  
}

멀티 스레드 데드락 - 번호 순서로 실행 되면 데드락 발생

#include <mutex>    
                                                                                                                                                                                                                                         
std::mutex mutex1;    
std::mutex mutex2; 

void deadlock_3_thread_1() {
    std::lock_guard<std::mutex> lockGuard(mutex1);    // 1   
    std::lock_guard<std::mutex> lockGuard(mutex2);    // 3
}                         
                          
void deadlock_3_thread_2() {
    std::lock_guard<std::mutex> lockGuard(mutex2);    // 2
    std::lock_guard<std::mutex> lockGuard(mutex1);    // 3
}
반응형
:

ubuntu c++ 맨페이지 설치

프로그래밍/c,c++ 2021. 11. 30. 17:07
반응형

터미널에서 간단히 c++ std api 내용 확인 할때

맨페이지 없는 상태

$ man std::lock_guard                        
No manual entry for std::lock_guard

맨페이지 설치

$ sudo apt install libstdc++-10-doc

설치된 맨페이지 확인

$ man std::lock_guard | cat
std::lock_guard< _Mutex >(3cxx)                                                                                                                                                                                                       std::lock_guard< _Mutex >(3cxx)

NAME
       std::lock_guard< _Mutex > - A simple scoped lock type.
......
반응형
:

책 "전문가를 위한 C++" - 1.2 C++의 고급기능

프로그래밍/c,c++ 2021. 1. 28. 11:29
반응형

"전문가를 위한 C++" 내용 정리

www.yes24.com/Product/Goods/77669043

 

전문가를 위한 C++

『전문가를 위한 C++』(개정4판)은 새로 추가된 기능과 관련 도구를 비롯해 C++에 대한 모든 내용을 코드와 솔루션 위주로 소개한다. 저자는 실전 프로그래밍에 적용할 수 있는 현실적인 기법을

www.yes24.com

string

#include <string>
#include <iostream>
using namespace std;
int main() {
    string myString = "Hello, world";

    cout << "string is " << myString << endl;
    cout << "second char is " << myString[1] << endl;

    return 0;
}

 

포인터와 동적 메모리 - malloc 과 free 대신 new, delete 사용

int* intPtr = nullptr;
intPtr = new int;
*intPtr = 5;
delete intPtr;
intPtr = nullptr;


동적으로 배열 할당하기

int arraySize = 8;
int* myArray = new int[arraySize];
delete[] myArray;
myArray = nullptr;

 

널 포인터 상수 - NULL 은 0 인지 널 포인터 인지 구분이 되지 않음

#include <iostream>
using namespace std;

void func(char* str) {
    cout << "char* " << endl;
}

void func(int num) {
    cout << "int " << endl;
}

int main() {
    // func(NULL);
    func(0);
    func(nullptr);

    return 0;
}

 

스마트 포인터
스마트 포인터를 사용하면 지정한 객체가 스코프를 벗어나면 메모리가 자동 해제됨(return 문 마다 메모리 해제 필요없음)

std::unique_ptr

#include <iostream>
#include <memory>
using namespace std;

class Employee {
    public:
        int salary;
        ~Employee() { cout << "~Employee" <<  endl; }
};

int main() {
    auto employee = make_unique<Employee>();
    if (employee) {
        cout << "salary " << employee->salary << endl;
    }

    auto employees = make_unique<Employee[]>(3);
    cout << "salary arr " << employees[0].salary << endl;

    return 0;
}



std::shared_ptr 대입연산이 발생할때마다 레퍼런스카운터 증가, 스코프를 벗어 나면 감소, 레퍼런스 카운터가 0 이 되면 메모리 해제됨

 auto employee = make_shared<Employee>();
    if (employee) {
        cout << "salary " << employee->salary << endl;
    }

 

const 상수 - #define 대신 const 를 사용한다.

레퍼런스 전달 - 일반 변수 처럼 사용하지만 내부적으로 해당 변수에 대한 포인터로 동작한다

int x = 42;
int& refer = x;

#include <iostream>
using namespace std;

void inc(int& i) {
    i++;
}

int main() {
    int a = 3;

    inc(a);
    inc(3); // ERROR
    cout << "a = " << a << endl;

    return 0;
}

 

타입 추론 - auto, decltype

#include <string>
#include <iostream>
using namespace std;

const string message = "TEST";
const string& foo() {
    return message;
}

int main() {
    auto f1 = foo();
    cout << "f1 = " << f1 << endl;

f1.append("a");

    cout << "f1 = " << f1 << endl;
    cout << "msg = " << message << endl;

    const auto& f2 = foo();
    cout << "f2 = " << f2 << endl;

f2.append("a");

    cout << "f2 = " << f2 << endl;
    cout << "msg = " << message << endl;

    return 0;
}
#include <string>
#include <iostream>
using namespace std;

const string message = "TEST";
const string& foo() {
    return message;
}

int main() {
    int x = 123;
    decltype(x) y = 456;

    decltype(foo()) f2 = foo();
    cout << "f2 = " << f2 << endl;

f2.append("a");

    cout << "f2 = " << f2 << endl;
    cout << "msg = " << message << endl;

    return 0;
}
반응형
:

[링크] 연산자 우선순위

프로그래밍/c,c++ 2021. 1. 27. 11:35
반응형

ko.wikipedia.org/wiki/C%EC%99%80_C%2B%2B%EC%9D%98_%EC%97%B0%EC%82%B0%EC%9E%90

반응형
:

책 "전문가를 위한 C++" - 1.1 C++의 기초

프로그래밍/c,c++ 2021. 1. 5. 17:33
반응형

"전문가를 위한 C++" 내용 정리

www.yes24.com/Product/Goods/77669043

 

전문가를 위한 C++

『전문가를 위한 C++』(개정4판)은 새로 추가된 기능과 관련 도구를 비롯해 C++에 대한 모든 내용을 코드와 솔루션 위주로 소개한다. 저자는 실전 프로그래밍에 적용할 수 있는 현실적인 기법을

www.yes24.com

namespace - 이름이 충돌하는 문제를 해결하기 위해 나온 개념
내가 작성한 코드의 함수명과 외부 라이브러리의 함수명이 같을 때
mycode.h

namespace mycode {
    void foo();
}

mycode.cpp

namespace mycode {
    void foo() {
        std::cout << "foo() called in the mycode namespace" << std::endl;
    }
}
mycode::foo();

using namespace mycode;
foo(); 

한 파일에서 using 을 여러개 사용하면 namespace를 사용하지 않는 것과 동일 한 상황이 됨

 

특정 항목에 대해서만 using 사용 가능함

using std::cout;
cout << "Hello world" << std::endl;

 

casting - c++ 방법 : i3

float myFloat = 3.14f;
int i1 = (int)myFloat;
int i2 = int(myFloat);
int i3 = static_cast<int>(myFloat);

연산자 - 우선순위 애매하면 ()

int i = 34 + 8 * 2 + 21 / 7 % 2;
int i = 34 + (8 * 2) + ((21 / 7) % 2);

enum 타입

const int PieceTypeKing = 0;
const int PieceTypeQueen = 1;
const int PieceTypeRook = 2;
const int PieceTypePawn = 3;
int myPiece = PieceTypeKing;

enum PieceType {
    PieceTypeKing,
    PieceTypeQueen,
    PieceTypeRook,
    PieceTypePawn
};
PieceType myPiece;
myPiece = 0; // ERROR

엄격한 타입(형변환 방지 및 사용 범위 지정)

enum class PieceType {
    King = 1,
    Queen,
    Rook = 10,
    Pawn
};

if (PieceType::Queen == 2) { ... } // ERROR
if (static_cast<int>(PieceType::Queen) == 2) { ... }

저장 되는 타입 지정 가능

enum class PieceType : unsigned long { 
    King = 1,
    Queen,
    Rook = 10,
    Pawn
};

 

bool 타입 : true, false

 

함수 리턴 타입 추론
여러개 return 이 있을 경우 모두 동일 타입이어야 함

auto addNumbers(int number1, int number2) {
    return number1 + number2;
}

 

배열 - c스타일 대신 std::array
장점 : 크기 명확, 자동 포인터 형변환 방지, 반복자로 배열 원소 접근

array<int, 3>arr = { 1, 2, 3 };
cout << "size = " << arr.size() << endl;
cout << "2nd = " << arr[1] << endl;

std::vector - 자동 확장

vector<int>vec = { 1, 2, 3 };
cout << "size = " << vec.size() << endl;
cout << "2nd = " << vec[1] << endl;
vec.push_back(4);
cout << "size = " << vec.size() << endl;
cout << "4th = " << vec[3] << endl;

 

범위 기반 for 문

std::array<int, 4> arr = { 1, 2, 3, 4};
for (int i : arr) {
    std::cout << i << std::endl;
}

int carr[4] = { 5, 6, 7, 8 };
for (int i : carr) {
    std::cout << i << std::endl;
}

 

initializer list
include <initializer_list>
클래스 템플릿, 타입 세이프

int a = makeSum({ 1, 2, 3 });
int b = makeSum({ 10, 20, 30, 40, 50, 60 });
// int c = makeSum({ 1, 2, 3.0 }); // ERROR
cout << "a = " << a << ", b = " << b << endl;
반응형
: