priority_queue
·
내배캠/C++
우선 순위 큐 priority_queue는 queue와 비슷하지만, 각 요소가 우선순위를 가지며 우선순위가 높은 요소가 먼저 처리되는 자료구조이다. 일반적인 FIFO(First Int First Out) 구조의 queue와는 다르게, 요소의 우선순위에 따라 정렬된 순서로 처리된다.삽입/삭제는 항상 o(long n) 이고, top() 가장 높은 우선순위 원소 조회는 o(1) 이다.그래서 가장 큰/작은 원소를 순식간에 추출해야하는 문제에 최적이다. 특징1. 정렬된 상태 유지:삽입되는 모든 요소는 우선 순위에 따라 자동으로 정렬된다.높은 우선순위의 요소는 큐의 앞쪽에 위치한다.2. 빠른 접근:우선순위를 통해 최대 값 혹은 최소 값을 빠르게 가져올 수 있다.3. Heap 기반 구현:보통 Max-Heap 혹은 M..
std::vector를 class로 구현
·
내배캠/C++
#include #include using namespace std;template class SimpleVector{private: T* Data; int CurrentSize; int CurrentCapacity;public: // 기본 생성자 SimpleVector() : CurrentSize(0), CurrentCapacity(10) { Data = new T[10](); } // 매개변수를 받는 생성자 SimpleVector(int Capacity) : CurrentSize(0), CurrentCapacity(Capacity) { Data = new T[CurrentCapacity](); } // 복사 생성자 SimpleVector(const SimpleVector& Other) : Curr..
std::regex_replace
·
내배캠/C++
regex_replace정규 표현식(regex)을 사용하여 문자열에서 특정 패턴을 찾아 다른 문자열로 교체하는 함수이다.C++ 표준 라이브러리의 헤더에 포함되어 있다. string regex_replace(const string& input, const regex& pattern, const string& replacement);input 문자열에서 pattern에 매칭되는 모든 부분을 replacement로 교체한 결과를 반환한다.input: 원본 문자열pattern: 찾고자 하는 패턴을 나타내는 정규식 객체replacement: 교체할 문자열  https://dong-grae.tistory.com/83 문자열에서 영문 숫자 변환하기*unordered_map 사용한 초기 코드더보기#include #i..
디자인 패턴(Observer Pattern)
·
내배캠/C++
#include #include #include using namespace std;// Observer 패턴에서 상태 변화를 알림 받는 객체들의 공통 인터페이스// Observer를 상속 받은 객체들은 이 인터페이스를 구현하여 'Update' 메서드를 통해 데이터를 전달 받는다class Observer{public: virtual ~Observer() = default; virtual void Update(int Data) = 0;};// Subject 클래스(액셀 시트 역할)// 데이터의 상태 변화를 관리하고, 모든 등록된 Observer에게 변경 사항을 알림class ExcelSheet{private: vector Observers; int Data;public: ExcelSheet() : Data..
디자인 패턴(Decorator Pattern)
·
내배캠/C++
#include #include using namespace std;class Pizza{public: virtual ~Pizza() {} virtual string GetName() const = 0; virtual double GetPrice() const = 0;};class BasicPizza : public Pizza{public: string GetName() const override { return "Basic Pizza"; } double GetPrice() const override { return 5.0; }};class PizzaDecorator : public Pizza{protected: Pizza* ..
디자인 패턴(Singleton Pattern)
·
내배캠/C++
#include #include using namespace std;class Airplane{private: static unique_ptr Instance; // 유일하게 객체를 관리할 Instance int PositionX, PositionY; // 복사 생성자 제한 Airplane(const Airplane&) = delete; // 대입 연산자 제한 Airplane& operator=(const Airplane&) = delete; Airplane() : PositionX(0), PositionY(0) { cout (new Airplane()); } return Instance.get(); // unique_ptr이 관리하는 객체의 포인터를 반환 } void Move(int DeltaX..