「🔨」 fix(ex01/ex02): fixed things that i forgot to do
This commit is contained in:
@@ -1,9 +1,3 @@
|
||||
#include <stdexcept>
|
||||
#include <algorithm>
|
||||
#include <limits>
|
||||
|
||||
#include <iostream>
|
||||
|
||||
#include "Span.hpp"
|
||||
|
||||
Span::Span(void)
|
||||
@@ -72,3 +66,17 @@ int Span::longestSpan(void)
|
||||
|
||||
return (*max - *min);
|
||||
}
|
||||
|
||||
std::ostream &operator<<(std::ostream &os, const Span &o)
|
||||
{
|
||||
os << "(size = " << o.getSize() << ") ";
|
||||
std::vector<int> const &numbers = o.getData();
|
||||
for (unsigned int i = 0; i < numbers.size(); i++)
|
||||
{
|
||||
os << numbers[i];
|
||||
if (i != numbers.size() - 1)
|
||||
os << ", ";
|
||||
}
|
||||
return os;
|
||||
}
|
||||
|
||||
|
||||
@@ -1,5 +1,9 @@
|
||||
#pragma once
|
||||
|
||||
#include <stdexcept>
|
||||
#include <algorithm>
|
||||
#include <iostream>
|
||||
#include <limits>
|
||||
#include <vector>
|
||||
|
||||
class Span {
|
||||
@@ -12,11 +16,29 @@ public:
|
||||
|
||||
void addNumber(const int &);
|
||||
|
||||
template <typename T>
|
||||
void addNumbers(const typename T::const_iterator begin, const typename T::const_iterator end)
|
||||
{
|
||||
unsigned int csize = this->_size;
|
||||
csize += std::distance(begin, end);
|
||||
if (csize > this->_max_size)
|
||||
throw std::length_error("iterator too big, can't add it");
|
||||
this->_data.insert(this->_data.end(), begin, end);
|
||||
this->_size += std::distance(begin, end);
|
||||
}
|
||||
|
||||
|
||||
|
||||
int shortestSpan(void);
|
||||
int longestSpan(void);
|
||||
|
||||
std::size_t getSize(void) const {return _size;}
|
||||
std::vector<int> getData(void) const {return _data;}
|
||||
|
||||
private:
|
||||
std::vector<int> _data;
|
||||
std::size_t _size;
|
||||
std::size_t _max_size;
|
||||
};
|
||||
|
||||
std::ostream &operator<<(std::ostream &os, const Span &s);
|
||||
|
||||
@@ -1,7 +1,9 @@
|
||||
#include "Span.hpp"
|
||||
#include <iostream>
|
||||
#include <list>
|
||||
|
||||
int main()
|
||||
{
|
||||
{
|
||||
Span sp = Span(5);
|
||||
sp.addNumber(6);
|
||||
@@ -9,7 +11,28 @@ int main()
|
||||
sp.addNumber(17);
|
||||
sp.addNumber(9);
|
||||
sp.addNumber(11);
|
||||
std::cout << sp.shortestSpan() << std::endl;
|
||||
std::cout << sp.longestSpan() << std::endl;
|
||||
|
||||
std::cout << sp << std::endl;
|
||||
std::cout << "shortest span: " << sp.shortestSpan() << std::endl;
|
||||
std::cout << "longest span: " << sp.longestSpan() << std::endl;
|
||||
}
|
||||
|
||||
std::cout << std::endl;
|
||||
|
||||
{
|
||||
std::list<int> list;
|
||||
for (int i = 0; i < 10; i++)
|
||||
{
|
||||
list.push_back(rand() % 100);
|
||||
}
|
||||
|
||||
Span sp(list.size());
|
||||
sp.addNumbers<std::list<int> >(list.begin(), list.end());
|
||||
|
||||
std::cout << sp << std::endl;
|
||||
std::cout << "shortest span: " << sp.shortestSpan() << std::endl;
|
||||
std::cout << "longest span: " << sp.longestSpan() << std::endl;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -1,8 +1,36 @@
|
||||
#include "MutantStack.hpp"
|
||||
#include <iostream>
|
||||
#include <list>
|
||||
|
||||
int main()
|
||||
{
|
||||
{
|
||||
std::cout << "control tests with a list:" << std::endl;
|
||||
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(727);
|
||||
mlist.push_back(0);
|
||||
|
||||
std::list<int>::iterator it = mlist.begin();
|
||||
std::list<int>::iterator ite = mlist.end();
|
||||
++it;
|
||||
--it;
|
||||
while (it != ite)
|
||||
{
|
||||
std::cout << *it << std::endl;
|
||||
++it;
|
||||
}
|
||||
}
|
||||
std::cout << std::endl;
|
||||
|
||||
{
|
||||
std::cout << "basic iterator tests:" << std::endl;
|
||||
MutantStack<int> mstack;
|
||||
mstack.push(5);
|
||||
mstack.push(17);
|
||||
@@ -11,18 +39,50 @@ int main()
|
||||
std::cout << mstack.size() << std::endl;
|
||||
mstack.push(3);
|
||||
mstack.push(5);
|
||||
mstack.push(737);
|
||||
//[...]
|
||||
mstack.push(727);
|
||||
mstack.push(0);
|
||||
|
||||
MutantStack<int>::iterator it = mstack.begin();
|
||||
MutantStack<int>::iterator ite = mstack.end();
|
||||
++it;
|
||||
--it;
|
||||
//*it = 20; // not a constant iterator, should be working if uncommented
|
||||
while (it != ite)
|
||||
{
|
||||
std::cout << *it << std::endl;
|
||||
++it;
|
||||
}
|
||||
std::stack<int> s(mstack);
|
||||
}
|
||||
std::cout << std::endl;
|
||||
|
||||
{
|
||||
std::cout << "const iterator tests:" << 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(727);
|
||||
mstack.push(0);
|
||||
|
||||
MutantStack<int>::const_iterator it = mstack.begin();
|
||||
MutantStack<int>::const_iterator ite = mstack.end();
|
||||
++it;
|
||||
--it;
|
||||
//*it = 20; // a constant iterator, should not work if uncommented
|
||||
while (it != ite)
|
||||
{
|
||||
std::cout << *it << std::endl;
|
||||
++it;
|
||||
}
|
||||
std::stack<int> s(mstack);
|
||||
}
|
||||
std::cout << std::endl;
|
||||
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user