Skip to content

1. 기초


1. 네임스페이스

#include <iostream>
using namespace std;

namespace silla {
int year = 935;

void CentralArea() {
    cout << "경상도" << endl;
}
}  // namespace silla

namespace baekjae {
int year = 660;

void CentralArea() {
    cout << "충청도" << endl;
}
}  // namespace baekjae

int main() {
    cout << "신라 중심지: ";
    silla::CentralArea();
    cout << "신라 멸망연도: " << silla::year << endl;

    cout << "백제 중심지: ";
    baekjae::CentralArea();
    cout << "백제 멸망연도: " << baekjae::year << endl;

    return 0;
}
신라 중심지: 경상도
신라 멸망연도: 935
백제 중심지: 충청도
백제 멸망연도: 660


2. #include

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

int main() {
    vector<int> exam;
    exam.push_back(10);
    exam.push_back(20);
    exam.push_back(30);

    for (int i = 0, size = exam.size(); i < size; i++) {
        cout << "벡터 값: " << exam.at(i) << endl;
    }

    return 0;
}
벡터 값: 10
벡터 값: 20
벡터 값: 30


3. 캐스트 연산자

#include <iostream>
using namespace std;

int main() {
    int x = 2;
    double y = 4.4;

    int i = static_cast<int>(y / x);
    int j = (int)y / x;
    double k = y / x;

    cout << "4.4 / 2 = (static_cast<int>) " << i << endl;
    cout << "4.4 / 2 = (int) " << j << endl;
    cout << "4.4 / 2 = " << k << endl;

    return 0;
}
4.4 / 2 = (static_cast<int>) 2
4.4 / 2 = (int) 2
4.4 / 2 = 2.2


4. 명시적 변환

#include <iostream>
using namespace std;

int main() {
    int number1 = 65;
    double number2 = 23.4;

    int number3 = int(number2);
    double number4 = double(number1 / number2);

    char ch = char(number1);

    cout << "number1: " << number1 << endl;
    cout << "number2: " << number2 << endl;
    cout << "number3: " << number3 << endl;
    cout << "number4: " << number4 << endl;
    cout << "ch: " << ch << endl;

    return 0;
}
number1: 65
number2: 23.4
number3: 23
number4: 2.77778
ch: A


5. 타입 크기

#include <iostream>
using namespace std;

class Temp {
    int no;
    bool is_on;
};

int main() {
    cout << "char 타입 크기: " << sizeof('p') << endl;
    cout << "int 타입 크기: " << sizeof(10) << endl;
    cout << "double 타입 크기: " << sizeof(10.0) << endl;
    cout << "Temp 클래스 크기: " << sizeof(Temp) << endl;

    return 0;
}
char 타입 크기: 1
int 타입 크기: 4
double 타입 크기: 8
Temp 클래스 크기: 8


6. 실수 소수점

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

int main() {
    cout << "== 소수점 버리기 ==" << endl;
    cout << "floor(1.1): " << floor(1.1) << endl;
    cout << "floor(2.3): " << floor(2.3) << endl;
    cout << "floor(-40.5): " << floor(-40.5) << endl;
    cout << "floor(-55.7): " << floor(-55.7) << endl;

    cout << "== 소수점 올리기 ==" << endl;
    cout << "ceil(1.1): " << ceil(1.1) << endl;
    cout << "ceil(2.3): " << ceil(2.3) << endl;
    cout << "ceil(-40.5): " << ceil(-40.5) << endl;
    cout << "ceil(-55.7): " << ceil(-55.7) << endl;

    cout << "== 소수점 반올림하기 ==" << endl;
    cout << "round(1.1): " << round(1.1) << endl;
    cout << "round(2.3): " << round(2.3) << endl;
    cout << "round(-40.5): " << round(-40.5) << endl;
    cout << "round(-55.7): " << round(-55.7) << endl;

    return 0;
}
== 소수점 버리기 ==
floor(1.1): 1
floor(2.3): 2
floor(-40.5): -41
floor(-55.7): -56
== 소수점 올리기 ==
ceil(1.1): 2
ceil(2.3): 3
ceil(-40.5): -40
ceil(-55.7): -55
== 소수점 반올림하기 ==
round(1.1): 1
round(2.3): 2
round(-40.5): -41
round(-55.7): -56


7. 절대값과 제곱수

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

int main() {
    cout << "== 절대값 구하기 ==" << endl;
    cout << "-10의 절대값: " << abs(-10) << endl;
    cout << "-5.72의 절대값: " << fabs(-5.72) << endl;

    cout << "== 제곱수 구하기 ==" << endl;
    cout << "2의 2승: " << pow(2, 2) << endl;
    cout << "3의 4승: " << pow(3, 4) << endl;

    return 0;
}
== 절대값 구하기 ==
-10의 절대값: 10
-5.72의 절대값: 5.72
== 제곱수 구하기 ==
2의 2승: 4
3의 4승: 81


8. 몫과 나머지

#include <iostream>
using namespace std;

int main() {
    double x = 5.7;
    int div1 = static_cast<int>(x / 5);
    double mod1 = x - 5 * static_cast<int>(x / 5);

    int y = 10;
    int div2 = static_cast<int>(y / 2);
    double mod2 = y % 2;

    cout << "5.7 / 5 = 몫: " << div1 << ", 나머지: " << mod1 << endl;
    cout << "10 / 2 = 몫: " << div2 << ", 나머지: " << mod2 << endl;

    return 0;
}
5.7 / 5 = 몫: 1, 나머지: 0.7
10 / 2 = 몫: 5, 나머지: 0


9. 제곱근

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

int main() {
    cout << "4의 제곱근: " << sqrt(4.0) << endl;
    cout << "16의 제곱근: " << sqrt(16) << endl;
    cout << "64의 제곱근: " << sqrt(64) << endl;
    cout << "256의 제곱근: " << sqrt(256.0) << endl;

    return 0;
}
4의 제곱근: 2
16의 제곱근: 4
64의 제곱근: 8
256의 제곱근: 16


10. 소수점 분리

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

int main() {
    double x = 1.2345;
    double div = 0.0;
    double mod = 0.0;

    mod = modf(x, &div);

    cout << "1.2345의 몫: " << div << ", 나머지: " << mod << endl;

    return 0;
}
1.2345의 몫: 1, 나머지: 0.2345


11. 난수 생성

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

int main() {
    srand(static_cast<unsigned int>(time(NULL)));

    for (int i = 0; i < 5; i++) {
        cout << "난수: " << rand() << endl;
    }

    return 0;
}
난수: 1421060440
난수: 1597176793
난수: 204772451
난수: 1341781463
난수: 595271494


12. 날짜와 시간 문자열 변환

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

int main() {
    time_t now = time(NULL);
    tm *ptm = localtime(&now);

    char buffer[100];
    strftime(buffer, 100, "예제 만드는 지금은 %Y년 %m월 %d일, %p %H시 %M분 %s초입니다.\n", ptm);

    cout << buffer;

    return 0;
}
예제 만드는 지금은 2022년 01월 12일, PM 15시 57분 1641970662초입니다.


13. 시간 측정

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

int main() {
    time_t start = time(NULL);
    time_t finish = time(NULL);

    int pass_int = 1;

    time(&start);

    for (int i = 1; i < 100000; i++) {
        for (int j = 1; j < 100000; j++) {
            pass_int += 1;
        }
    }

    time(&finish);

    cout << "1을 100억 번 더하는 시간: "
         << difftime(finish, start) << "초" << endl;

    return 0;
}
1을 100억 번 더하는 시간: 5초


14. 함수 오버로딩

#include <iostream>
using namespace std;

int Plus(int arg1, int arg2) {
    return arg1 + arg2;
}

double Plus(double arg1, double arg2, double arg3) {
    return arg1 + arg2 + arg3;
}

int main() {
    int number1 = Plus(2, 4);
    double number2 = Plus(3.4, 5.7, 8.4);

    cout << "number1: " << number1 << endl;
    cout << "number2: " << number2 << endl;

    return 0;
}
number1: 6
number2: 17.5


15. 디폴트 매개변수

#include <iostream>
using namespace std;

int GetOne() { return 1; }

int Plus(int x, int y = GetOne(), int z = 1) {
    return x + y + z;
}

int main() {
    int number1 = Plus(1);
    int number2 = Plus(1, 2);
    int number3 = Plus(1, 2, 3);

    cout << "결과값: " << number1 << ", "
         << number2 << ", " << number3 << endl;

    return 0;
}
결과값: 3, 4, 6


16. 가변인자 함수

#include <stdarg.h>

#include <iostream>
using namespace std;

int Sum(int args, ...) {
    va_list ap;
    va_start(ap, args);

    int sum = 0;

    for (int i = 0; i < args; i++)
        sum += va_arg(ap, int);

    va_end(ap);

    return sum;
}

int main() {
    int number = Sum(5, 1, 2, 3, 4, 5);

    cout << "1에서 5까지 합: " << number << endl;

    return 0;
}
1에서 5까지 합: 15


17. 인라인 함수

#include <iostream>
using namespace std;

inline int Max(int x, int y) {
    return x > y ? x : y;
}

int main() {
    for (int i = 0; i < 5; i++)
        cout << Max(i, i + 10) << endl;

    return 0;
}
10
11
12
13
14


18. typedef

#include <iostream>
using namespace std;

typedef enum State {
    OPEN,
    CLOSE,
    DISCONNECT
} state_;

struct Status {
    state_ machine1;
    state_ machine2;
} status_;

int main() {
    status_.machine1 = OPEN;
    status_.machine2 = DISCONNECT;

    cout << "machine1 상태: " << status_.machine1 << endl;
    cout << "machine2 상태: " << status_.machine2 << endl;

    return 0;
}
machine1 상태: 0
machine2 상태: 2


19. using

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

namespace MyArea {
int Plus(int x, int y) { return x + y; }
}  // namespace MyArea

using namespace MyArea;
using MyVector = vector<int>;

int main() {
    MyVector data = MyVector();
    data.push_back(1);
    data.push_back(2);

    cout << "Data: " << data.at(0) << ", " << data.at(1) << endl;
    cout << "Plus: " << Plus(2, 4) << endl;
}
Data: 1, 2
Plus: 6


20. auto

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

int GetInt() { return 1; }
double GetDouble() { return 1.11; }
string GetString() { return "3"; }

auto add(int x, int y) -> int {
    return x + y;
}

int main() {
    auto data1 = GetInt();
    auto data2 = GetDouble();
    auto data3 = GetString();
    auto data4 = add(5.1, 10.2);

    cout << "Data1: " << data1 << ", "
         << typeid(data1).name() << endl;
    cout << "Data2: " << data2 << ", "
         << typeid(data2).name() << endl;
    cout << "Data3: " << data3 << ", "
         << typeid(data3).name() << endl;
    cout << "Data4: " << data4 << ", "
         << typeid(data4).name() << endl;

    return 0;
}
Data1: 1, i
Data2: 1.11, d
Data3: 3, NSt3__112basic_stringIcNS_11char_traitsIcEENS_9allocatorIcEEEE
Data4: 15, i


21. decltype

#include <iostream>
using namespace std;

int main() {
    auto data1 = 1;
    decltype(data1) data2 = 2;
    auto data3 = 3.4;
    decltype(data2 + data3) data4 = 3;

    cout << "Data1: " << data1 << ", "
         << typeid(data1).name() << endl;
    cout << "Data2: " << data2 << ", "
         << typeid(data2).name() << endl;
    cout << "Data3: " << data3 << ", "
         << typeid(data3).name() << endl;
    cout << "Data4: " << data4 << ", "
         << typeid(data4).name() << endl;

    return 0;
}
Data1: 1, i
Data2: 2, i
Data3: 3.4, d
Data4: 3, d


22. constexpr

#include <iostream>
using namespace std;

constexpr int GetNumber1() {
    return 2;
}

constexpr int GetNumber2(int x, int y) {
    return x + y;
}

int main() {
    int array1[GetNumber1()];
    int array2[GetNumber2(1, 2)];

    cout << sizeof(array1) << ", "
         << sizeof(array2) << endl;

    return 0;
}
8, 12


23. 람다

#include <iostream>
using namespace std;

auto func1 = []() { cout << "Lambda Function" << endl; };
auto func2 = [](int x, int y) -> bool { return x < y; };

int main() {
    int x = 2;

    auto func3 = [=](int y) {
        func1();
        cout << "x < y = " << func2(x, y) << endl;
    };

    func3(4);

    auto func4 = [=](int y) { return x * x + y * y; };

    cout << "x * x + y * y = " << func4(5) << endl;

    return 0;
}
Lambda Function
x < y = 1
x * x + y * y = 29


24. 실수 0 확인

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

int main() {
    double number1 = 1;
    double number2 = 0.4;
    double number3 = 0.0;
    double number4 = 11.2;

    cout << boolalpha;
    cout << isnormal(number1) << endl;
    cout << isnormal(number2) << endl;
    cout << isnormal(number3) << endl;
    cout << isnormal(number4) << endl;

    return 0;
}
true
true
false
true


25. NAN 확인

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

int main() {
    double number1 = 1;
    double number2 = 0.4;
    double number3 = 0.0;
    double number4 = 11.2;

    cout << boolalpha;
    cout << isnan(number1 / number4) << endl;
    cout << isnan(number2 / number3) << endl;
    cout << isnan(number3 / number3) << endl;
    cout << isnan(number4 / number1) << endl;

    return 0;
}
false
false
true
false


26. 좌변과 우변 크기 확인

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

int main() {
    int x = 10;
    int y = 20;
    double a = 12.2;
    double b = 5.6;

    cout << boolalpha;

    cout << "== 좌변 > 우변 ==" << endl;
    cout << isgreater(20, 11) << endl;
    cout << isgreater(x, y) << endl;
    cout << isgreater(a, b) << endl;
    cout << isgreater(x, b) << endl;

    cout << "== 좌변 < 우변 ==" << endl;
    cout << isless(20, 11) << endl;
    cout << isless(x, y) << endl;
    cout << isless(a, b) << endl;
    cout << isless(x, b) << endl;

    return 0;
}
== 좌변 > 우변 ==
true
false
true
true
== 좌변 < 우변 ==
false
true
false
false


27. 두 숫자 차이 확인

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

int main() {
    cout << fdim(-1.0, 0.0) << endl;
    cout << fdim(1.0, 0.0) << endl;
    cout << fdim(0.0, 0.0) << endl;
    cout << fdim(5, 1) << endl;
    cout << fdim(6, 15) << endl;
    cout << fdim(4.9, 1.1) << endl;
    cout << fdim(5.9, 20.1) << endl;

    return 0;
}
0
1
0
4
0
3.8
0