CPP08 1.0
読み取り中…
検索中…
一致する文字列を見つけられません
main.cpp
[詳解]
1/* ************************************************************************** */
2/* */
3/* ::: :::::::: */
4/* main.cpp :+: :+: :+: */
5/* +:+ +:+ +:+ */
6/* By: kamitsui <kamitsui@student.42tokyo.jp> +#+ +:+ +#+ */
7/* +#+#+#+#+#+ +#+ */
8/* Created: 2025/08/22 00:05:24 by kamitsui #+# #+# */
9/* Updated: 2025/08/22 00:06:56 by kamitsui ### ########.fr */
10/* */
11/* ************************************************************************** */
12
25#include "MutantStack.hpp"
26#include <iostream>
27#include <list>
28#include <vector>
29
34 std::cout << "--- Subject Main Test ---" << std::endl;
35 MutantStack<int> mstack;
36
37 mstack.push(5);
38 mstack.push(17);
39
40 std::cout << "Top element: " << mstack.top() << std::endl; // Should be 17
41
42 mstack.pop(); // Removes 17
43
44 std::cout << "Stack size after pop: " << mstack.size() << std::endl; // Should be 1
45
46 mstack.push(3);
47 mstack.push(5);
48 mstack.push(737);
49 mstack.push(0);
50
51 std::cout << "\nIterating through MutantStack:" << std::endl;
53 MutantStack<int>::iterator ite = mstack.end();
54
55 ++it;
56 --it;
57 while (it != ite) {
58 std::cout << *it << std::endl;
59 ++it;
60 }
61
62 // Test copy constructor and assignment with std::stack
63 std::cout << "\nCreating a std::stack from MutantStack..." << std::endl;
64 std::stack<int> s(mstack);
65 std::cout << "Original std::stack size: " << s.size() << std::endl;
67}
68
73 std::cout << "--- Comparison with std::list ---" << std::endl;
74 std::list<int> list;
75
76 list.push_back(5);
77 list.push_back(17);
78
79 std::cout << "Back element: " << list.back() << std::endl;
80
81 list.pop_back();
82
83 std::cout << "List size after pop: " << list.size() << std::endl;
84
85 list.push_back(3);
86 list.push_back(5);
87 list.push_back(737);
88 list.push_back(0);
89
90 std::cout << "\nIterating through std::list:" << std::endl;
92 std::list<int>::iterator ite = list.end();
93
94 ++it;
95 --it;
96 while (it != ite) {
97 std::cout << *it << std::endl;
98 ++it;
99 }
101}
102
107 std::cout << "--- Reverse Iterator Test ---" << std::endl;
108 MutantStack<char> mstack;
109 mstack.push('a');
110 mstack.push('b');
111 mstack.push('c');
112 mstack.push('d');
113
114 std::cout << "MutantStack (forward): ";
115 for (MutantStack<char>::iterator it = mstack.begin(); it != mstack.end(); ++it) {
116 std::cout << *it << " ";
117 }
119
120 std::cout << "MutantStack (reverse): ";
121 for (MutantStack<char>::reverse_iterator rit = mstack.rbegin(); rit != mstack.rend(); ++rit) {
122 std::cout << *rit << " ";
123 }
125}
126
131 const int NUM_OPERATIONS = 1000000;
132 std::cout << "--- Performance Test (" << NUM_OPERATIONS << " push/pop operations) ---" << std::endl;
133
134 // 1. Default (std::deque)
135 MutantStack<int> stack_with_deque;
136 clock_t start_deque = clock();
137 for (int i = 0; i < NUM_OPERATIONS; ++i) {
138 stack_with_deque.push(i);
139 }
140 for (int i = 0; i < NUM_OPERATIONS; ++i) {
141 stack_with_deque.pop();
142 }
143 clock_t end_deque = clock();
144 double duration_deque = static_cast<double>(end_deque - start_deque) / CLOCKS_PER_SEC;
145 std::cout << "Internal container std::deque: " << duration_deque << " seconds" << std::endl;
146
147 // 2. std::vector
148 MutantStack<int, std::vector<int> > stack_with_vector;
149 clock_t start_vector = clock();
150 for (int i = 0; i < NUM_OPERATIONS; ++i) {
151 stack_with_vector.push(i);
152 }
153 for (int i = 0; i < NUM_OPERATIONS; ++i) {
154 stack_with_vector.pop();
155 }
156 clock_t end_vector = clock();
157 double duration_vector = static_cast<double>(end_vector - start_vector) / CLOCKS_PER_SEC;
158 std::cout << "Internal container std::vector: " << duration_vector << " seconds" << std::endl;
159
160 // 3. std::list
161 MutantStack<int, std::list<int> > stack_with_list;
162 clock_t start_list = clock();
163 for (int i = 0; i < NUM_OPERATIONS; ++i) {
164 stack_with_list.push(i);
165 }
166 for (int i = 0; i < NUM_OPERATIONS; ++i) {
167 stack_with_list.pop();
168 }
169 clock_t end_list = clock();
170 double duration_list = static_cast<double>(end_list - start_list) / CLOCKS_PER_SEC;
171 std::cout << "Internal container std::list: " << duration_list << " seconds" << std::endl;
172}
173
179int main() {
184 return 0;
185}
Provides a stack container that can be accessed by iterators.
T back(T... args)
T begin(T... args)
reverse_iterator rend()
Returns a reverse iterator pointing to the second-to-last element in the stack.
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.
T end(T... args)
T endl(T... args)
int main()
Program entry point.
Definition main.cpp:49
void reverse_iterator_test()
Tests the behaviour of reverse iterators.
Definition main.cpp:106
void performanceTest()
Test to compare performance using internal containers.
Definition main.cpp:130
void list_comparison_test()
Test comparing MutantStack's behaviour with std::list.
Definition main.cpp:72
void subject_main_test()
Basic test cases specified in the assignment.
Definition main.cpp:33
T pop_back(T... args)
T pop(T... args)
T push_back(T... args)
T push(T... args)
T size(T... args)
T top(T... args)