
1. WHAT
- 문자 -> int 형변환
// char -> int 변환
int n = char - '0';
// string -> int 변환
int n = stoi(string);
- 벡터 특정 원소 제거
벡터.erase(remove(벡터.begin(), 벡터.end(), 특정원소), 벡터.end());
//remove는 특정 원소를 건너뛰기하며 재배치 후 유효한 끝의 iterator 반환
//erase를 통해 반환된 iterator ~ end 전까지 제거 되는 것
std::remove는 특정 값을 제거하는 것이 아니라, 조건을 만족하는 원소들을 앞으로 이동시키고 나머지 영역은 정의하지 않는다.
- const
함수앞에 const -> 반환값이 변경되지않음
함수 뒤에 const -> 객체 내부 값이 변경되지않음
- foreach문에서 참조하기!!!! for(int num : vec) -> for(int& num : vec)
2. RESULT
- 미니 실습
싱글톤
단 한 번 객체를 생성하여 전역에서 이를 공유한다. 효율적인 자원 관리와 데이터의 일관성을 제공한다. 게임 매니저, 오디오 매니저, 데이터 매니저 등 게임 전체에서 단 하나만 존재해야하고 전역적으로 접근해야하는 시스템을 구현할 때 사용한다.
데코레이터
기존 코드를 수정하지 않고 새로운 기능을 추가하거나 수정할 수 있다. 총알/무기 강화, 캐릭터 능력치 증가 등 상속 대신 실행 중 꾸미기를 통해 새로운 기능을 덧붙여 원래 객체를 확장할 때 사용한다.
옵저버
주체와 옵저버는 서로에 대해 정보를 몰라도 상호작용을 할 수 있어 객체 간의 결합도가 낮다. 새 옵저버를 쉽게 추가 가능하고 주체의 동작 변경 없이 여러 옵저버가 관찰 할 수 있는 확장성이 높다. 플레이어 체력 변화에 따른 UI 업데이트, 아이템 획득 알림, 퀘스트 진행 상황 통보 등에 사용된다.
- 도전 실습
template <typename T>
class Singleton {
private:
static T* instance;
protected:
Singleton() {}
public:
static T* getInstance() {
if (instance == nullptr) {
instance = new T();
}
return instance;
}
};
- 3주차 숙제

#include <iostream>
#include <vector>
#include <string>
using namespace std;
// 고객 인터페이스 (Observer 역할)
class Customer {
public:
virtual void update(const string& status) = 0; // 순수 가상 함수
};
// 일반 고객 클래스
class RegularCustomer : public Customer {
private:
string name;
public:
RegularCustomer(const string& name) : name(name) {}
void update(const string& status) {
cout << "Regular customer " << name << " received update: " << status << endl;
}
};
// TODO: VIP 고객 클래스 (Customer를 상속받아 구현)
// 요구 사항:
// - 고객 이름을 저장하는 멤버 변수 `name`을 추가하세요.
// - 생성자에서 이름을 초기화하세요.
// - `update` 메서드를 구현하여 "VIP customer [이름] received VIP update: [배송 상태]" 형식으로 출력되도록 하세요.
class VIPCustomer : public Customer {
private:
string name;
public:
VIPCustomer(const string& name) : name(name) {}
void update(const string& status) {
cout << "VIP customer " << name << " received VIP update: " << status << endl;
}
};
class BusinessCustomer : public Customer {
private:
string companyName;
public:
BusinessCustomer(const string& companyName) :companyName(companyName) {}
void update(const string& status) {
cout << "Business customer " << companyName << " received business update: " << status << endl;
}
};
// TODO: 배송 회사 클래스 (DeliveryService)
// 요구 사항:
// - `customers`라는 고객 리스트를 저장하는 멤버 변수를 추가하세요.
// - `currentStatus`라는 현재 배송 상태를 저장하는 멤버 변수를 추가하세요.
// - 고객을 추가하는 `addCustomer` 메서드를 구현하세요.
// - 고객을 제거하는 `removeCustomer` 메서드를 구현하세요.
// - 배송 상태를 업데이트하고 모든 고객에게 알리는 `updateStatus` 메서드를 구현하세요.
// - 등록된 모든 고객에게 상태를 전달하는 `notifyCustomers` 메서드를 구현하세요.
class DeliveryService {
private:
vector<Customer*> customers;
string currentStatus;
public:
void addCustomer(Customer* customer) {
customers.push_back(customer);
}
void removeCustomer(Customer* customer) {
customers.erase(remove(customers.begin(), customers.end(), customer), customers.end());
}
void updateStatus(const string& status) {
currentStatus = status;
notifyCustomers();
}
void notifyCustomers() {
for (Customer* cus : customers) {
cus->update(currentStatus);
}
}
};
// Main 함수
int main() {
DeliveryService service;
// 고객 객체 생성
RegularCustomer* customer1 = new RegularCustomer("Alice");
VIPCustomer* customer2 = new VIPCustomer("Bob");
BusinessCustomer* customer3 = new BusinessCustomer("CompanyX");
// 고객 등록
service.addCustomer(customer1);
service.addCustomer(customer2);
service.addCustomer(customer3);
// 배송 상태 업데이트 및 알림
cout << "Updating status: 배송 준비 중" << endl;
service.updateStatus("배송 준비 중");
cout << "\nUpdating status: 배송 완료" << endl;
service.updateStatus("배송 완료");
// 메모리 해제
delete customer1;
delete customer2;
delete customer3;
return 0;
}

3. 참고
https://youtu.be/v7kF3fweRH8?si=-ymw1_sFHhmSne5Q
[C++] vector 특정 원소 지우기
vector에서 원소 하나를 삭제하는 시간복잡도는 O(N)이다. 값으로 삭제 vector v; 라고 선언된 벡터에서 특정한 원소를 지울 때 아래와 같이 할 수 있습니다. v.erase(remove(v.begin(), v.end(), 지우고 싶은 원
mangu.tistory.com
() {} [] <> - = + _ 다 오른손 중지로 치는 습관이 있어서 ... 습관 고치려고 타자 연습을 했는데 새끼손가락으로 괄호를 입력하는 느낌이 너무 이상하다........... @@.. https://www.speedcoder.net/
Typing Practice for Programmers | SpeedCoder
www.speedcoder.net

'내배캠Unreal_TIL > C++' 카테고리의 다른 글
| [TIL] 2026-01-12 | C++ 프로그래머스 행렬의 덧셈, 직사각형 별찍기, 최대공약수와 최소공배수 (0) | 2026.01.12 |
|---|---|
| [TIL] 2025-12-29 | C++에서 중요한 건 메모리, 수명, 비용 (0) | 2025.12.29 |
| [TIL] 2025-12-22 | C++ 디자인패턴 - 싱글톤, 데코레이터, 옵저버 정리 (0) | 2025.12.22 |
| [TIL] 2025-12-19 | 과제4 구현 (0) | 2025.12.19 |
| [C++] STL 총정리 (0) | 2025.12.18 |