스택 및 대기열

스택: 후입선출(후입선출이 먼저 사용됨)

#include <iostream>
#include <string>
#include <stack>		// 스택

int main() {
	std::stack<int> s1, s2;

	std::cout << s1.empty();	// s1 스택이 비어있으면 1 아니면 0 호출

	s1.push(1); s1.push(2); s1.push(3);	// s1 스택에다가 값 집어넣기
	s2.push(10); s2.push(9); s2.push(8); s2.push(7); s2.push(6);	// s2 스택에다가 값 집어넣기

	std::cout << " / " << s2.empty() << std::endl;	// s2 스택이 비어있으면 1 아니면 0 호출

	std::cout << "--------------------" << std::endl;
	std::cout << s1.size() << std::endl;	// s1 스택의 원소 개수
	std::cout << s2.size() << std::endl;	// s2 스택의 원소 개수
	std::cout << "--------------------" << std::endl;
	std::cout << s1.top() << std::endl;		// s1 스택의 맨 위에 값 호출
	std::cout << s2.top() << std::endl;		// s2 스택의 맨 위에 값 호출
	
	s1.swap(s2);	// s1과 s2 서로 교체
	std::cout << "--------------------" << std::endl;
	std::cout << s1.size() << std::endl;	// s1 스택의 원소 개수
	std::cout << s2.size() << std::endl;	// s2 스택의 원소 개수
	std::cout << "--------------------" << std::endl;
	std::cout << s1.top() << std::endl;		// s1 스택의 맨 위에 값 호출
	std::cout << s2.top() << std::endl;		// s2 스택의 맨 위에 값 호출
	std::cout << "--------------------" << std::endl;
	
	std::stack<std::string> str_s;

	str_s.emplace("Hellow World!");
	str_s.emplace("My name is!");

	std::cout << str_s.size() << std::endl;	// str_s 스택의 원소 개수

	while (str_s.empty()==0)
	{
		std::cout << str_s.top() << '\n';	// str_s의 맨 위에 값 출력
		str_s.pop();						// str_s의 맨 위에 값 제거
	}
}

Q: 선입선출(선입선출)

#include <iostream>
#include <string>
#include <queue>		// 큐

int main() {
	std::queue<int> q1, q2;
	
	for (int i = 5; i <= 10; i++) {
		q1.push(i);	// q1에다가 5부터 10까지 집어넣기
	}
	std::cout << q1.empty() << std::endl; // q1 큐가 비어있으면 1 아니면 0 출력
	std::cout << q1.size() << std::endl;	// q1 큐의 크기를 출력
	std::cout << q1.front() << std::endl;	// 맨 앞에 있는 값 출력
	std::cout << q1.back() << std::endl;	// 맨 뒤에 있는 값 출력
	q1.pop();	// 맨 앞에 있는 값 삭제
	std::cout << q1.front() << std::endl;	// 맨 앞에 있는 값 출력

	q1.swap(q2);		// q2하고 교체
	std::cout << q1.empty() << std::endl; // q1 큐가 비어있으면 1 아니면 0 출력
	std::cout << q1.size() << std::endl;	// q1 큐의 크기를 출력

	std::queue<std::string> str_q;

	str_q.emplace("안녕하세요?");
	str_q.emplace("저의 이름은");
	str_q.emplace("abcdefg");

	std::cout << str_q.size() << std::endl;	// str_q 큐의 크기를 출력
	while (!str_q.empty())			
	{
		std::cout << str_q.front() << std::endl;		// str_q의 맨 앞에 값 출력
		str_q.pop();	// str_q의 맨 앞에 값 삭제
	}
}