![thumbnail](https://img1.daumcdn.net/thumb/R750x0/?scode=mtistory2&fname=https%3A%2F%2Fblog.kakaocdn.net%2Fdn%2FcevvBj%2Fbtq7C3D0gUd%2FA48r7eNbRx0cNVh8r2Yrkk%2Fimg.png)
[C++] 예제로 알아보는 std::pair, std::tuple
2021. 6. 20. 20:09
C++/공부
std::pair https://en.cppreference.com/w/cpp/utility/pair std::pair - cppreference.com template struct pair; std::pair is a class template that provides a way to store two heterogeneous objects as a single unit. A pair is a specific case of a std::tuple with two elements. If neither T1 nor T2 is a possibly c en.cppreference.com #include #include #include std::pair Login(int ..
![thumbnail](https://img1.daumcdn.net/thumb/R750x0/?scode=mtistory2&fname=https%3A%2F%2Fblog.kakaocdn.net%2Fdn%2Fypmhv%2Fbtq1uvfcuUA%2FEIWn1LUv8ZtHzxKPgzKKO0%2Fimg.jpg)
[C++] 모던 C++로 배우는 함수형 프로그래밍 - (1-3) 람다 표현식
2021. 4. 1. 22:30
C++/모던 C++로 배우는 함수형 프로그래밍
간단한 함수를 람다로 표현하기 람다 표현식: 함수의 익명 표기법. 함수형 프로그래밍에서 일급 함수(first class function)와 순수 함수(pure function)를 만들 때 유용하다. [...] (...) {...} [] - 캡처 목록(capturing list) 람다 표현식 바깥의 변수를 람다 본문에서 어떻게 접근하는지를 나타낸다. 람다 표현식의 시작을 나타낸다. 캡처 목록에 &를 사용하면 바깥 범위에 있는 변수를 참조로 캡처하고, =를 쓰면 값으로 캡처한다. () - 매개변수 목록(parameter list) {} - 본문 for_each( begin(vehicles), end(vehicles), [](const Vehicle &vehicle) { cout
![thumbnail](https://img1.daumcdn.net/thumb/R750x0/?scode=mtistory2&fname=https%3A%2F%2Fblog.kakaocdn.net%2Fdn%2FbM5o8z%2Fbtq1z32sHi0%2FhWzW0KAmRzUqsTNWW8foGk%2Fimg.jpg)
[C++] 모던 C++로 배우는 함수형 프로그래밍 - (1-2) C++ 표준 라이브러리 구현 살펴보기
2021. 3. 31. 22:48
C++/모던 C++로 배우는 함수형 프로그래밍
컨테이너 안에 객체 배치하기 컨테이너: 다른 객체를 저장하고, 이 객체가 사용하는 메모리를 관리한다. std::array - 동일한 데이터 타입의 객체를 저장하고 선형으로 배치하는 순차 컨테이너 // 10개의 정수로 array를 초기화한다. std::array arr = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 }; // array의 각 요소를 출력한다. for (auto a : arr) std::cout
![thumbnail](https://img1.daumcdn.net/thumb/R750x0/?scode=mtistory2&fname=https%3A%2F%2Fblog.kakaocdn.net%2Fdn%2FbXVQm8%2Fbtq1nIlspr6%2FV2sBMmVksKar2DV1KePl9K%2Fimg.jpg)
[C++] 모던 C++로 배우는 함수형 프로그래밍 - (1-1) 모던 C++의 새로운 기능 익히기
2021. 3. 30. 23:48
C++/모던 C++로 배우는 함수형 프로그래밍
Auto 키워드로 데이터 타입을 자동으로 정의하기 자동 지속 기간(Automatic Storage Duration): 변수가 해당 변수를 정의한 시점에서 생성되고, 코드 블록을 빠져나가면 파괴되는 것 C++11부터는 auto 키워드가 초기화에 선언된 변수의 실제 타입을 추론하기 위해 사용된다. C++14부터는 auto 키워드를 함수에 적용해 후행 반환 타입(Trailing Return Type)을 사용할 수 있다. auto a = 1; auto b = 1.0; auto c = a + b; auto d = { b, c }; std::cout
![thumbnail](https://img1.daumcdn.net/thumb/R750x0/?scode=mtistory2&fname=https%3A%2F%2Fblog.kakaocdn.net%2Fdn%2FoGe87%2FbtqGeaVQTX0%2FClNKn7gdQ8n6tkI8mvr4QK%2Fimg.png)
[C++] <functional>을 이용한 버튼 구현
2020. 8. 2. 23:07
C++/공부
UI 구현 시 이곳저곳에 버튼이 참 많이 들어간다. 이 경우 차라리 하나의 클래스로 뽑아서, 버튼이 클릭되면 지정한 함수를 호출하는 기능을 하게끔 구현하려고 하였다. 함수 포인터를 이용하니 static 선언이 없으면 불가능했으며, static으로 선언된 멤버 함수에서는 멤버 변수 및 다른 멤버 함수에 접근하는 게 불가능하기 때문에 원하는 동작을 원활하게 실행할 수가 없었다. std::function, std::bind 나의 경우 의 std::function, std::bind를 이용하였다. class button { // ... function _callbackFunction; } 생성자나 함수를 이용하여 _callbackFunction을 초기화하면 된다. 이때 인스턴스의 바인딩이 필요함에 주의한다. _..
![thumbnail](https://img1.daumcdn.net/thumb/R750x0/?scode=mtistory2&fname=https%3A%2F%2Fblog.kakaocdn.net%2Fdn%2FXBD5O%2FbtqErKkwhrL%2FdYac0pf80bFPFCI8JRqiM0%2Fimg.png)
[C++] 전방 선언에 관하여
2020. 5. 26. 22:43
C++/공부
전방 선언: 선 실행 후 연결 전방 선언은 불필요한 헤더 파일이 복잡하게 포함되는 것을 방지하여, 결과적으로는 컴파일 속도를 향상시킵니다. 객체 수가 많다는 가정하에 빌드 속도 차이가 확연하게 납니다. 예제와 함께 알아보기 예를 들어 클래스 A를 지갑, 클래스 B를 통장이라고 했을 때, 클래스 AB는 지갑과 통장에 모두 접근할 수 있는 클래스입니다. 따라서 지갑에 있는 돈을 꺼내어 사용하기도 하고, 통장에 있는 돈을 꺼내서 지갑에 넣기도(또는 그 반대) 하겠죠? 아무런 기능도 하지 않는 클래스들을 만들어, 예시와 함께 보겠습니다. AB.h #pragma once #include "A.h" #include "B.h" class AB { }; A.h #pragma once class A { }; B.h #p..
![thumbnail](https://img1.daumcdn.net/thumb/R750x0/?scode=mtistory2&fname=https%3A%2F%2Fblog.kakaocdn.net%2Fdn%2Fq4dqx%2FbtqEh7T2pKi%2F2dTpAFGPVykr9VoR3AS7J1%2Fimg.png)
[C++] 포인터를 이용하여 for문 별 찍기
2020. 5. 17. 18:08
C++/공부
코드 #include using namespace std; int main() { const char* star= "*****"; const char* space= " "; for (int i = 0; i < 5; i++) { cout
![thumbnail](https://img1.daumcdn.net/thumb/R750x0/?scode=mtistory2&fname=https%3A%2F%2Fblog.kakaocdn.net%2Fdn%2Fba3ORR%2FbtqEgQ6CAlG%2FdcjdRjr7Lxsf4ahj5HnS3k%2Fimg.png)
[C++] 셔플(Shuffle) 알고리즘
2020. 5. 16. 21:19
C++/공부
코드 #include #include using namespace std; int main() { srand(time(NULL)); int number[10]; int dest, sour, temp; for (int i = 0; i < 10; i++) number[i] = i + 1; // 셔플 알고리즘 // i의 조건은 섞고 싶은 만큼 지정하기 for (int i = 0; i < 77; i++) { dest = rand() % 10; sour = rand() % 10; temp = number[dest]; number[dest] = number[sour]; number[sour] = temp; } for (int i = 0; i < 10; i++) cout
![thumbnail](https://img1.daumcdn.net/thumb/R750x0/?scode=mtistory2&fname=https%3A%2F%2Fblog.kakaocdn.net%2Fdn%2FTdzK2%2FbtqEUpng0gh%2FpQ5yffgjzf5Yjk2C8pjONK%2Fimg.png)
[C++] 따라하며 배우는 C++ - (14) 예외 처리
2020. 5. 5. 19:23
C++/따라하며 배우는 C++
예외처리의 기본 #include #include #include using namespace std; int findFirstChar(const char * string, char ch) { for (std::size_t index = 0; index < strlen(string); ++index) if (string[index] == ch) return index; return -1;// no match } // 리턴값을 bool로 처리할 수도 있음 double divide(int x, int y, bool &success) { if (y == 0) { success = false; return 0.0; } success = false; return static_cast(x) / y; } int mai..
![thumbnail](https://img1.daumcdn.net/thumb/R750x0/?scode=mtistory2&fname=https%3A%2F%2Fblog.kakaocdn.net%2Fdn%2FcWB16t%2FbtqEIUsZqw7%2FaKDFgtWGL9p1xQ2dksq6nK%2Fimg.png)
[C++] 따라하며 배우는 C++ - (13) 템플릿
2020. 5. 4. 13:59
C++/따라하며 배우는 C++
함수 템플릿 프로그래머의 단순 반복 작업을 줄여 주기 위한 장치가 있다. 템플릿은 여러 가지 자료형에 대해서 비슷한 코드를 반복하는 것을 방지해 준다. Cents.h #pragma once #include class Cents { private: int m_cents; public: Cents(int cents) : m_cents(cents) { } friend bool operator > (const Cents &c1, const Cents &c2) { return (c1.m_cents > c2.m_cents); } friend std::ostream& operator
![thumbnail](https://img1.daumcdn.net/thumb/R750x0/?scode=mtistory2&fname=https%3A%2F%2Fblog.kakaocdn.net%2Fdn%2FU09aG%2FbtqEvYYBR48%2Fz5nrwD4mto3s8ccbJADCBK%2Fimg.png)
[C++] 따라하며 배우는 C++ - (12) 가상 함수들
2020. 5. 3. 20:28
C++/따라하며 배우는 C++
다형성의 기본 개념 자식 클래스 객체에 부모 클래스 포인터를 사용한다면? #include #include using namespace std; class Animal { protected: string m_name; public: Animal(std::string name) : m_name(name) {} public: string getName() { return m_name; } //void speak() const virtual void speak() const { cout
![thumbnail](https://img1.daumcdn.net/thumb/R750x0/?scode=mtistory2&fname=https%3A%2F%2Fblog.kakaocdn.net%2Fdn%2FczZr6G%2FbtqEqr7DChk%2Fi4eohlIpOItXnnJKwY7Okk%2Fimg.png)
[C++] 따라하며 배우는 C++ - (11) 상속
2020. 5. 2. 19:14
C++/따라하며 배우는 C++
상속의 기본 (1) Inheritance(is-a relationship) #include using namespace std; class Mother// generalized class { private:// 자식한테도 허용X //public:// 다 열어 버림 //protected:// private 상태를 유지하면서 자식에게는 허용 int m_i; public: Mother(const int & i_in) : m_i(i_in) { std::cout
![thumbnail](https://img1.daumcdn.net/thumb/R750x0/?scode=mtistory2&fname=https%3A%2F%2Fblog.kakaocdn.net%2Fdn%2Fdn5qwO%2FbtqElXySddw%2FcxYieQaPljL9kXBKy0mukK%2Fimg.png)
[C++] 따라하며 배우는 C++ - (10) 객체들 사이의 관계에 대해
2020. 4. 30. 01:36
C++/따라하며 배우는 C++
객체들의 관계 1. 프로그램이 수행해야 하는 기능 정하기 2. 어떤 객체들이 어떻게 나눠서 도움을 주고받을지 설계 3. 설계에 따라서 여러 클래스를 구현 구성(요소) 관계 Composition Part-of 두뇌는 육체의 일부이다. 육체 없이는 존재할 수 없으며, 두뇌가 육체 전체에 대해 알고 있지는 않다. 전체/부품 - 육체/두뇌 다른 클래스에도 속할 수 없음 - 두뇌는 다른 클래스에 속할 수 없음 멤버의 존재를 클래스가 관리함 - 두뇌를 육체가 관리함 단방향 Position2D.h #pragma once #include class Position2D { private: int m_x; int m_y; public: Position2D(const int & x_in, const int & y_in) :..
[C++] cin.eof()와 EOF(End of file)
2020. 4. 28. 22:56
C++/공부
EOF EOF(End of File): 파일의 끝, 더 이상 읽을 데이터가 없다 cin으로 입력을 받으려고 할 때, EOF라면 입력이 취소되고 cin.eof()는 true를 반환한다. 이를 이용하여 파일이 종료될 때까지 입력을 받는 코드를 작성할 수 있다. 터미널(콘솔)에서는 EOF를 수동으로 넣어 주어야 한다. Windows: Ctrl+z / Unix: Ctrl+d 코드 #include int main() { using namespace std; int a, b; while(true) { cin >> a >> b; if (cin.eof()) break; cout
![thumbnail](https://img1.daumcdn.net/thumb/R750x0/?scode=mtistory2&fname=https%3A%2F%2Fblog.kakaocdn.net%2Fdn%2FofEIG%2FbtqEj80kL4X%2F6qCJlkJ1jlLTRXSKYUG1p1%2Fimg.png)
[C++] 따라하며 배우는 C++ - (9) 연산자 오버로딩
2020. 4. 27. 21:44
C++/따라하며 배우는 C++
산술 연산자 오버로딩 하기 #include using namespace std; // 사용자 정의 자료형에서도 산술 연산자 정의 가능 class Cents { private: int m_cents; public: Cents(int cents = 0) { m_cents = cents; } int getCents() const { return m_cents; } int& getCents() { return m_cents; } //friend Cents operator + (const Cents &c1, const Cents &c2) //{ //return Cents(c1.getCents() + c2.getCents()); //} // friend 없애고 멤버 함수로 사용하려면... this 사용 // 멤버 ..
![thumbnail](https://img1.daumcdn.net/thumb/R750x0/?scode=mtistory2&fname=https%3A%2F%2Fblog.kakaocdn.net%2Fdn%2FRmHn8%2FbtqDXFj5CVn%2F0PDtrDheNzmQgGtDSl9yx1%2Fimg.png)
[C++] 따라하며 배우는 C++ - (8) 객체지향의 기초
2020. 4. 25. 02:37
C++/따라하며 배우는 C++
객체지향 프로그래밍과 클래스 #include #include #include using namespace std; // Friend: name, address, age, height, weight, ... void print(const string &name, const string &address, const int &age, const double &height, const double &weight) { cout
![thumbnail](https://img1.daumcdn.net/thumb/R750x0/?scode=mtistory2&fname=https%3A%2F%2Fblog.kakaocdn.net%2Fdn%2FQjBvy%2FbtqDQbKhcsG%2FOg8V9hNADJC5tPvhypLq21%2Fimg.png)
[C++] 따라하며 배우는 C++ - (7) 함수
2020. 4. 23. 17:10
C++/따라하며 배우는 C++
매개변수와 실인자의 구분 매개변수(Parameter) 인자(Argument) #include using namespace std; int foo(int x, int y); // parameter: 함수가 어떤 기능을 하는지, 바꿔 주는 역할 int foo(int x, int y) { // like... int x, y; return x + y; } // x and y are destroyed here, 함수가 끝남과 동시에 소멸됨 int main() { int x = 1, y = 2; foo(6, 7);// 6, 7: arguments (actual parameters) foo(x, y + 1);// x에 있는 값이 argument로, 함수의 parameter로 들어간다 // 항상 x에 있는 값만 전달되..
![thumbnail](https://img1.daumcdn.net/thumb/R750x0/?scode=mtistory2&fname=https%3A%2F%2Fblog.kakaocdn.net%2Fdn%2FbSq2RJ%2FbtqDIn5alEx%2FY6ltKE4wSWCpgPkw0fIrQ0%2Fimg.png)
[C++] 따라하며 배우는 C++ - (6) 행렬, 문자열, 포인터, 참조
2020. 4. 22. 21:24
C++/따라하며 배우는 C++
배열 기초 array #include using namespace std; #define D_NUM_STUDENTS 100000 struct Rectangle { int length; int width; }; enum StudentName { JACKJACK,// = 0 DASH,// = 1 VIOLET,// = 2 NUM_STUDENTS,// = 3 }; int main() { /* int student1_score;// jack jack int student2_score;// dash int student3_score;// violet */ int one_student_score;// 1 variable int student_scores[5];// 5 int cout