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;
}
2. #include
3. 캐스트 연산자
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;
}
5. 타입 크기
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;
}
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;
}
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;
}
9. 제곱근
10. 소수점 분리
11. 난수 생성
12. 날짜와 시간 문자열 변환
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;
}
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;
}
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;
}
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;
}
17. 인라인 함수
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;
}
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;
}
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;
}
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;
}
22. constexpr
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;
}
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;
}
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;
}
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;
}
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;
}