CPP08 1.0
読み取り中…
検索中…
一致する文字列を見つけられません
MutantStack.hpp
[詳解]
1/* ************************************************************************** */
2/* */
3/* ::: :::::::: */
4/* MutantStack.hpp :+: :+: :+: */
5/* +:+ +:+ +:+ */
6/* By: kamitsui <kamitsui@student.42tokyo.jp> +#+ +:+ +#+ */
7/* +#+#+#+#+#+ +#+ */
8/* Created: 2025/08/21 23:50:50 by kamitsui #+# #+# */
9/* Updated: 2025/08/22 00:19:29 by kamitsui ### ########.fr */
10/* */
11/* ************************************************************************** */
12
18#ifndef MUTANTSTACK_HPP
19#define MUTANTSTACK_HPP
20
21#include <deque>
22#include <iostream>
23#include <stack>
24
25/*
26 * @class MutantStack
27 * @brief An iterable stack container.
28 * @details This class inherits from std::stack and exposes iterators from the
29 * underlying container, allowing it to be used in range-based loops
30 * and with algorithms that require iterators.
31 * @tparam T The type of the elements.
32 * @tparam Container The type of the underlying container, defaults to std::deque<T>.
33 */
34template <typename T, typename Container = std::deque<T> > class MutantStack : public std::stack<T, Container> {
35 public:
36 // --- Canonical Form ---
37
39 MutantStack() : std::stack<T, Container>() {}
40
45 MutantStack(const MutantStack<T, Container> &other) : std::stack<T, Container>(other) {}
46
51 void swap(MutantStack &other) {
52 // Swap the protected member 'c' of the parent class using std::swap.
53 std::swap(this->c, other.c);
54 }
55
65 this->swap(other);
66 return *this;
67 }
68
71
72 // --- Iterator Support ---
73 // By exposing the underlying container's iterators, we make the stack iterable.
74 // The underlying container is a protected member of std::stack named 'c'.
75 typedef typename Container::iterator iterator;
76 typedef typename Container::const_iterator const_iterator;
77 typedef typename Container::reverse_iterator reverse_iterator;
78 typedef typename Container::const_reverse_iterator const_reverse_iterator;
79
81 iterator begin() { return this->c.begin(); }
83 iterator end() { return this->c.end(); }
84
86 const_iterator begin() const { return this->c.begin(); }
88 const_iterator end() const { return this->c.end(); }
89
91 reverse_iterator rbegin() { return this->c.rbegin(); }
93 reverse_iterator rend() { return this->c.rend(); }
94
96 const_reverse_iterator rbegin() const { return this->c.rbegin(); }
98 const_reverse_iterator rend() const { return this->c.rend(); }
99};
100
101#endif
102// ---- Before 1 Refactor ----
103//
104// // Assignment Operator
105// MutantStack<T, Container> &operator=(const MutantStack<T, Container> &other) {
106// if (this != &other) {
107// // Use the base class's assignment operator
108// this->c = other.c; // Directly manipulate protected member c.
109// }
110// return *this;
111// }
112//
113// ---- Before 2 Refactor ----
114//
115// // Assignment Operator
116// MutantStack &operator=(const MutantStack &other) {
117// std::stack<T, Container>::operator=(other); // Call the assignment operator of the parent class.
118// return *this;
119// }
120//
121// ---- Memo ----
122// MutantStack<T, Container> : official name
123// MutantStack : abbreviation name
MutantStack(const MutantStack< T, Container > &other)
Copy constructor.
const_iterator begin() const
Returns a const iterator pointing to the bottom (first element) of the stack.
void swap(MutantStack &other)
Swaps the contents of two MutantStack objects.
MutantStack< T, Container > & operator=(MutantStack< T, Container > other)
Assignment operator (copy-and-swap idiom).
const_reverse_iterator rbegin() const
Returns a const reverse iterator pointing to the top (last element) of the stack.
reverse_iterator rend()
Returns a reverse iterator pointing to the second-to-last element in the stack.
~MutantStack()
Destructor
Container::const_iterator const_iterator
const_reverse_iterator rend() const
Returns a const reverse iterator pointing to the second-to-last element in the stack.
const_iterator end() const
Returns a const iterator pointing to the end of the stack (after the last element).
iterator begin()
Returns an iterator pointing to the bottom (first element) of the stack.
Container::reverse_iterator reverse_iterator
Container::iterator iterator
iterator end()
Returns an iterator pointing to the end of the stack (after the last element).
reverse_iterator rbegin()
Returns a reverse iterator pointing to the top (last element) of the stack.
Container::const_reverse_iterator const_reverse_iterator
MutantStack()
Default Constructor
T swap(T... args)