GameChoi
Choi Programming
GameChoi
전체 방문자
오늘
어제
  • 분류 전체보기 (468)
    • C++ Algorithm & Study (184)
      • C++ & Algorithm Strategies (45)
      • Game Math & DirectX 11 (72)
      • Server + UE5 (29)
      • Lyra Clone Coding (37)
    • Create Game (284)
      • [Window API] Game Client & .. (55)
      • [DirectX] DirectX 2D & 3D (155)
      • [UE5] BLUEPRINT & C++ (74)
    • odds and ends (0)
      • English (0)

블로그 메뉴

  • 홈
  • 태그
  • 방명록

공지사항

인기 글

태그

  • Other Character
  • UE5
  • session
  • Direct3D
  • Player State
  • Game Room
  • RPG Game
  • core
  • Destination Move Packet
  • job queue
  • server
  • protobuf
  • GAME Client
  • c++
  • client
  • Player Move Packet
  • Direct11
  • Algorithm Strategies
  • Game Server
  • Network Worker

최근 댓글

최근 글

티스토리

hELLO · Designed By 정상우.
GameChoi

Choi Programming

[C++] 8 - 기초 문법 공부 일지(Stack)
C++ Algorithm & Study/C++ & Algorithm Strategies

[C++] 8 - 기초 문법 공부 일지(Stack)

2022. 12. 2. 04:44
  • Stack
    • .LIFO - 후입선출 규칙을 따르는 자료 구조

Stack.h

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

template<typename T>
class Stack
{
public:
	void	push(const T& value) { _container.push_back(value); }
	void	pop() 
	{
		vector<T> newVector(_container.size() - 1);
		for (int i = 0; i < _container.size() - 1; i++)
			newVector[i] = _container[i];

		_container.clear();
		_container = newVector;
	}
	
	T&		top() { return _container[_container.size() - 1]; }
	bool	empty() { return size() > 0; }
	int		size() { return _container.size(); }

	void	Print()
	{
		for (auto data : _container)
			cout << data << " " << endl;
	}


private:
	vector<T> _container;
};

main.cpp

#include "pch.h"
#include "Stack.h"

int main()
{
	Stack<int> stack;
	stack.push(100);
	stack.push(200);
	stack.push(300);
	stack.push(400);

	cout << stack.top() << endl;
	stack.pop();

	stack.Print();
}
  • Queue
    • .FIFO - 선입선출 규칙을 따르는 자료 구조

Queue.h

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

template<typename T>
class Queue
{
public:
	explicit	Queue() { _container.resize(100); }
	~Queue() { _container.clear(); _front = 0; _back = 0; _size = 0; }

	void		push(const T& value)
	{
		if (_size == _container.size())
		{
			vector<T> newVector(_container.size() - 1);
			newVector.resize(_size * 1.5);

			for (int i = 0; i < _size; i++)
				newVector[i] = _container[i];

			_container.clear();
			_container = newVector;
		}

		_container[_back] = value;
		_back = (_back + 1) % _container.size();
		_size++;
	}
	void		pop() { _front = (_front + 1) % _container.size(); _size--; }
	
	T&			front() { return _container[_front]; }
	bool		empty() { return _size == 0; }
	int			size() { return _size; }


	void		Print()
	{
		for (int i = _front; i < _back;i++)
			cout << _container[i] << " " << endl;
	}
private:
	vector<T>	_container;

	int			_front = 0;
	int			_back = 0;
	int			_size = 0;
};

main.cpp

#include "pch.h"
#include "Queue.h"

int main()
{
	Queue<int> queue;

	queue.push(100);
	queue.push(200);
	queue.push(300);

	queue.pop();

	queue.Print();
}
  • PriorityQueue
    • 우선순위에 따라 정렬된 Queue
    • Tree   - heap 구조 사용

PriorityQueue.h

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

template<typename T, typename Predicate = less<T>>
class PriorityQueue
{
public:
	void push(const T& value)
	{
		_heap.push_back(value);

		int child = _heap.size() - 1;
		while (child > 0)
		{
			int parent = (child - 1) / 2; // now 의 부모 노드
			if (_predicate(_heap[child], _heap[parent])) break;

			std::swap(_heap[child], _heap[parent]);
			child = parent;
		}
	}
	void pop()
	{
		_heap[0] = _heap.back(); // 마지막 값 첫번째로
		_heap.pop_back(); // 마지막 값 제거

		int now = 0; // 처음부터 자리 찾기
		while (true)
		{
			int left = 2 * now + 1;
			int right = 2 * now + 2;
			int next = now;
			if (left >= _heap.size()) break; // 마지막 값 도달
			
			if (_predicate(_heap[next], _heap[left])) next = left;
			if (right < _heap.size() && _predicate(_heap[next], _heap[right])) next = right;
			if (next == now) break;

			std::swap(_heap[now], _heap[next]);
		}

	}

	void Print()
	{
		for (auto data : _heap)
			cout << data << " " << endl;
	}

	T& top() { return _heap[0]; }
	bool empty() { return _heap.empty();}

private:
	vector<T> _heap;
	Predicate _predicate;
};

main.cpp

#include "pch.h"
#include "Queue.h"

int main()
{
	PriorityQueue<int, greater<int>> queue;

	queue.push(100);
	queue.push(200);
	queue.push(300);

	queue.pop();
	queue.Print();
}

'C++ Algorithm & Study > C++ & Algorithm Strategies' 카테고리의 다른 글

[C++] 10 - 기초 문법 공부 일지(Tree)  (0) 2022.12.05
[C++] 9 - 기초 문법 공부 일지(Graph)  (2) 2022.12.03
[C++] 7 - 기초 문법 공부 일지(Array)  (0) 2022.12.01
[C++] 6 - 기초 문법 공부 일지(동적 할당)  (0) 2022.12.01
[C++] 5 - 기초 문법 공부 일지(CAST)  (0) 2022.12.01
    'C++ Algorithm & Study/C++ & Algorithm Strategies' 카테고리의 다른 글
    • [C++] 10 - 기초 문법 공부 일지(Tree)
    • [C++] 9 - 기초 문법 공부 일지(Graph)
    • [C++] 7 - 기초 문법 공부 일지(Array)
    • [C++] 6 - 기초 문법 공부 일지(동적 할당)
    GameChoi
    GameChoi

    티스토리툴바