diff --git a/cpp08/ex01/Span.cpp b/cpp08/ex01/Span.cpp index 7d0fabb..f6ce5ae 100644 --- a/cpp08/ex01/Span.cpp +++ b/cpp08/ex01/Span.cpp @@ -40,6 +40,21 @@ Span::~Span() { std::cout << CLR_MAGENTA << "[Span] Destructor called" << CLR_RESET << std::endl; } +/** + * @brief Add a number at the end of the vector + * + * @param number the number to add + */ +void Span::addNumbers(std::vector::iterator start, std::vector::iterator end) { + size_t count = std::distance(start, end); + if (_max < _size + count) { + throw std::out_of_range("Span is full"); + } + _size += count; + + _vec.insert(_vec.end(), start, end); +} + /** * @brief Add a number at the end of the vector * diff --git a/cpp08/ex01/main.cpp b/cpp08/ex01/main.cpp index 5035ffe..3890fa5 100644 --- a/cpp08/ex01/main.cpp +++ b/cpp08/ex01/main.cpp @@ -6,7 +6,7 @@ /* By: rparodi +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/04/03 14:09:32 by rparodi #+# #+# */ -/* Updated: 2025/04/03 15:21:18 by rparodi ### ########.fr */ +/* Updated: 2025/04/04 15:07:59 by rparodi ### ########.fr */ /* */ /* ************************************************************************** */ @@ -24,8 +24,24 @@ int main() { std::cout << CLR_BLUE << "Shortest span: " << CLR_GOLD << sp.shortestSpan() << CLR_RESET << std::endl; std::cout << CLR_BLUE << "Longest span: " << CLR_GOLD << sp.longestSpan() << CLR_RESET << std::endl; } catch (std::exception &e) { - std::cerr << CLR_RED << "Exception: " << e.what() << CLR_RESET << std::endl; + std::cerr << CLR_RED << "Error: " << e.what() << CLR_RESET << std::endl; } + try { + std::cout << std::endl<< CLR_YELLOW << "[ AddNumbers method ]" << CLR_RESET << std::endl; + Span sp = Span(2); + std::vector vec; + vec.push_back(7); + vec.push_back(13); + sp.addNumbers(vec.begin(), vec.end()); + std::cout << CLR_BLUE << "Shortest span: " << CLR_GOLD << sp.shortestSpan() << CLR_RESET << std::endl; + std::cout << CLR_BLUE << "Longest span: " << CLR_GOLD << sp.longestSpan() << CLR_RESET << std::endl; + vec.clear(); + vec.push_back(42); + vec.push_back(21); + sp.addNumbers(vec.begin(), vec.end()); + } catch (std::exception(&e)) { + std::cerr << CLR_RED << "Error: " << e.what() << CLR_RESET << std::endl; + } return 0; }