From 52a866baba3cbb05ecf0be88f1b50991b4a26b52 Mon Sep 17 00:00:00 2001 From: Charles Date: Wed, 15 Apr 2020 09:28:09 +0200 Subject: cpp08 probably done, ex02 has a useless file in this implementation? --- cpp08/ex02/main.cpp | 45 ++++++++++++++++ cpp08/ex02/mutantstack.cpp | 15 ++++++ cpp08/ex02/mutantstack.hpp | 130 +++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 190 insertions(+) create mode 100644 cpp08/ex02/main.cpp create mode 100644 cpp08/ex02/mutantstack.cpp create mode 100644 cpp08/ex02/mutantstack.hpp (limited to 'cpp08/ex02') diff --git a/cpp08/ex02/main.cpp b/cpp08/ex02/main.cpp new file mode 100644 index 0000000..f3b1ec5 --- /dev/null +++ b/cpp08/ex02/main.cpp @@ -0,0 +1,45 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* main.cpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: charles +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/04/15 07:30:43 by charles #+# #+# */ +/* Updated: 2020/04/15 09:21:12 by charles ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include +#include "mutantstack.hpp" + +int main() +{ + MutantStack mstack; + + mstack.push(5); + mstack.push(17); + + std::cout << mstack.top() << std::endl; + + mstack.pop(); + + std::cout << mstack.size() << std::endl; + + mstack.push(3); + mstack.push(5); + mstack.push(737); + mstack.push(0); + MutantStack::iterator it = mstack.begin(); + MutantStack::iterator ite = mstack.end(); + + ++it; + --it; + while (it != ite) + { + std::cout << *it << std::endl; + ++it; + } + std::stack s(mstack); + return 0; +} diff --git a/cpp08/ex02/mutantstack.cpp b/cpp08/ex02/mutantstack.cpp new file mode 100644 index 0000000..47cc71b --- /dev/null +++ b/cpp08/ex02/mutantstack.cpp @@ -0,0 +1,15 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* mutantstack.cpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: charles +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/04/15 07:45:13 by charles #+# #+# */ +/* Updated: 2020/04/15 09:24:23 by charles ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "mutantstack.hpp" + +// I'm missing something? diff --git a/cpp08/ex02/mutantstack.hpp b/cpp08/ex02/mutantstack.hpp new file mode 100644 index 0000000..2cbeb9e --- /dev/null +++ b/cpp08/ex02/mutantstack.hpp @@ -0,0 +1,130 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* mutantstack.hpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: charles +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/04/15 07:29:42 by charles #+# #+# */ +/* Updated: 2020/04/15 09:25:55 by charles ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef MUTANTSTACK_HPP +# define MUTANTSTACK_HPP + +# include + +template +class MutantStack : public std::stack +{ +public: + MutantStack() : std::stack() + {} + + MutantStack(MutantStack const& other) : std::stack(other) + {} + + void operator=(MutantStack const& other) + { + std::stack::operator=(other); + } + + ~MutantStack() + {} + + class iterator + { + public: + iterator(iterator const& other) + : m_parentStack(other.m_parentStack), m_pos(other.m_pos) + {} + + void operator=(iterator const& other) + { + m_parentStack = other.m_parentStack; + m_pos = other.m_pos; + } + + ~iterator() + {} + + iterator(MutantStack& parentStack, unsigned int n) + : m_parentStack(parentStack), m_pos(n) + {} + + T& operator*() + { + std::stack tmp; + + for (unsigned int i = m_pos; i != 0; i--) + { + tmp.push(m_parentStack.top()); + m_parentStack.pop(); + } + T& res = m_parentStack.top(); + while (tmp.size() != 0) + { + m_parentStack.push(tmp.top()); + tmp.pop(); + } + return res; + } + // T& operator->(); + + // pre increment; + iterator& operator++() + { + m_pos++; + return *this; + } + + // post increment; + iterator& operator++(int) + { + m_pos++; + return *this; + } + + iterator& operator--() + { + m_pos--; + return *this; + } + + iterator& operator--(int) + { + m_pos--; + return *this; + } + + bool operator==(iterator const& right) + { + return m_pos == right.m_pos; + } + + bool operator!=(iterator const& right) + { + return !(*this == right); + } + + private: + iterator() : m_pos(0) + {} + + MutantStack& m_parentStack; + int m_pos; + }; + + iterator begin() + { + return iterator(*this, 0); + } + + iterator end() + { + return iterator(*this, this->size()); + } +}; + +#endif -- cgit