push
This commit is contained in:
parent
bf4a571b4b
commit
93f06c3b6d
|
|
@ -30,6 +30,7 @@ void Span::addNumber(int x)
|
||||||
|
|
||||||
if (this->_vec.size() >= this->_n)
|
if (this->_vec.size() >= this->_n)
|
||||||
throw std::out_of_range("Span vector is full");
|
throw std::out_of_range("Span vector is full");
|
||||||
|
|
||||||
if (this->_vec.size() < this->_n)
|
if (this->_vec.size() < this->_n)
|
||||||
this->_vec.push_back(x);
|
this->_vec.push_back(x);
|
||||||
}
|
}
|
||||||
|
|
@ -38,6 +39,7 @@ int Span::longestSpan() const
|
||||||
{
|
{
|
||||||
if (_vec.empty() || _vec.size() == 1)
|
if (_vec.empty() || _vec.size() == 1)
|
||||||
throw std::out_of_range("Span vector has less than 2 values");
|
throw std::out_of_range("Span vector has less than 2 values");
|
||||||
|
|
||||||
std::vector<int>::const_iterator maxIt = std::max_element(_vec.begin(), _vec.end());
|
std::vector<int>::const_iterator maxIt = std::max_element(_vec.begin(), _vec.end());
|
||||||
std::vector<int>::const_iterator minIt = std::min_element(_vec.begin(), _vec.end());
|
std::vector<int>::const_iterator minIt = std::min_element(_vec.begin(), _vec.end());
|
||||||
return (*maxIt - *minIt);
|
return (*maxIt - *minIt);
|
||||||
|
|
@ -47,10 +49,13 @@ int Span::shortestSpan() const
|
||||||
{
|
{
|
||||||
if (_vec.empty() || _vec.size() == 1)
|
if (_vec.empty() || _vec.size() == 1)
|
||||||
throw std::out_of_range("Span vector has less than 2 values");
|
throw std::out_of_range("Span vector has less than 2 values");
|
||||||
|
|
||||||
std::vector<int> tempvec = _vec;
|
std::vector<int> tempvec = _vec;
|
||||||
std::sort(tempvec.begin(), tempvec.end());
|
std::sort(tempvec.begin(), tempvec.end());
|
||||||
int minspan = std::numeric_limits<int>::max();
|
int minspan = std::numeric_limits<int>::max();
|
||||||
for (size_t i = 0; i < _vec.size() - 1; ++i) {
|
|
||||||
|
for (size_t i = 0; i < _vec.size() - 1; ++i)
|
||||||
|
{
|
||||||
int tempspan = tempvec[i + 1] - tempvec[i];
|
int tempspan = tempvec[i + 1] - tempvec[i];
|
||||||
if (tempspan < minspan)
|
if (tempspan < minspan)
|
||||||
minspan = tempspan;
|
minspan = tempspan;
|
||||||
|
|
|
||||||
BIN
ex01/obj/Main.o
BIN
ex01/obj/Main.o
Binary file not shown.
BIN
ex01/obj/Span.o
BIN
ex01/obj/Span.o
Binary file not shown.
|
|
@ -0,0 +1,50 @@
|
||||||
|
#include "MutantStack.hpp"
|
||||||
|
#include <list>
|
||||||
|
|
||||||
|
int main()
|
||||||
|
{
|
||||||
|
// TEST 1 - MutantStack
|
||||||
|
std::cout << "test 1" << std::endl << std::endl;
|
||||||
|
MutantStack<int> 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<int>::iterator it = mstack.begin();
|
||||||
|
MutantStack<int>::iterator ite = mstack.end();
|
||||||
|
++it;
|
||||||
|
--it;
|
||||||
|
while (it != ite) {
|
||||||
|
std::cout << *it << std::endl;
|
||||||
|
++it;
|
||||||
|
}
|
||||||
|
std::stack<int> s(mstack);
|
||||||
|
|
||||||
|
std::cout << std::endl << "test 2" << std::endl << std::endl;
|
||||||
|
// TEST 2 - std::list (même comportement attendu)
|
||||||
|
std::list<int> mlist;
|
||||||
|
mlist.push_back(5);
|
||||||
|
mlist.push_back(17);
|
||||||
|
std::cout << mlist.back() << std::endl;
|
||||||
|
mlist.pop_back();
|
||||||
|
std::cout << mlist.size() << std::endl;
|
||||||
|
mlist.push_back(3);
|
||||||
|
mlist.push_back(5);
|
||||||
|
mlist.push_back(737);
|
||||||
|
mlist.push_back(0);
|
||||||
|
std::list<int>::iterator it2 = mlist.begin();
|
||||||
|
std::list<int>::iterator ite2 = mlist.end();
|
||||||
|
++it2;
|
||||||
|
--it2;
|
||||||
|
while (it2 != ite2) {
|
||||||
|
std::cout << *it2 << std::endl;
|
||||||
|
++it2;
|
||||||
|
}
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,36 @@
|
||||||
|
CXX = c++
|
||||||
|
CXXFLAGS = -Wall -Wextra -Werror -std=c++98
|
||||||
|
OBJDIR = obj
|
||||||
|
SOURCES = Main.cpp
|
||||||
|
OBJECTS = $(addprefix $(OBJDIR)/, $(SOURCES:.cpp=.o))
|
||||||
|
NAME = Mstack
|
||||||
|
|
||||||
|
all: $(NAME)
|
||||||
|
|
||||||
|
$(OBJDIR):
|
||||||
|
@echo "📁 Creating obj directory..."
|
||||||
|
@mkdir -p $(OBJDIR)
|
||||||
|
|
||||||
|
$(OBJDIR)/%.o: %.cpp | $(OBJDIR)
|
||||||
|
@echo "🧠 Compiling $< ..."
|
||||||
|
@$(CXX) $(CXXFLAGS) -c $< -o $@
|
||||||
|
@echo "✅ $@ ready!"
|
||||||
|
|
||||||
|
$(NAME): $(OBJECTS)
|
||||||
|
@echo "🔗 Linking $(NAME) ..."
|
||||||
|
@$(CXX) $(CXXFLAGS) $(OBJECTS) -o $(NAME)
|
||||||
|
@echo "🎉 $(NAME) is ready!"
|
||||||
|
|
||||||
|
clean:
|
||||||
|
@echo "🧹 Cleaning object files..."
|
||||||
|
@rm -rf $(OBJDIR)
|
||||||
|
@echo "✨ Objects cleaned!"
|
||||||
|
|
||||||
|
fclean: clean
|
||||||
|
@echo "🗑️ Removing $(NAME)..."
|
||||||
|
@rm -f $(NAME)
|
||||||
|
@echo "💀 Full clean complete!"
|
||||||
|
|
||||||
|
re: fclean all
|
||||||
|
|
||||||
|
.PHONY: all clean fclean re
|
||||||
|
|
@ -0,0 +1,30 @@
|
||||||
|
#ifndef MUTANTSTACK_HPP
|
||||||
|
#define MUTANTSTACK_HPP
|
||||||
|
|
||||||
|
#include <stack>
|
||||||
|
#include <iostream>
|
||||||
|
template <typename T>
|
||||||
|
class MutantStack : public std::stack<T>
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
MutantStack() {};
|
||||||
|
~MutantStack() {};
|
||||||
|
MutantStack(const MutantStack &other) {this = other;};
|
||||||
|
MutantStack &operator=(const MutantStack &other);
|
||||||
|
|
||||||
|
typedef typename std::stack<T>::container_type::iterator iterator;
|
||||||
|
typedef typename std::stack<T>::container_type::const_iterator const_iterator;
|
||||||
|
typedef typename std::stack<T>::container_type::reverse_iterator reverse_iterator;
|
||||||
|
typedef typename std::stack<T>::container_type::const_reverse_iterator const_reverse_iterator;
|
||||||
|
|
||||||
|
iterator begin() {return this->c.begin();}
|
||||||
|
iterator end() {return this->c.end();}
|
||||||
|
const_iterator begin() const {return this->c.begin();}
|
||||||
|
const_iterator end() const {return this->c.end();}
|
||||||
|
reverse_iterator rbegin() {return this->c.rbegin();}
|
||||||
|
reverse_iterator rend() {return this->c.rend();}
|
||||||
|
const_reverse_iterator rbegin() const {return this->c.rbegin();}
|
||||||
|
const_reverse_iterator rend() const {return this->c.rend();}
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif
|
||||||
Loading…
Reference in New Issue