diff --git a/common.mk b/common.mk index e1f8179..2e13a39 100644 --- a/common.mk +++ b/common.mk @@ -15,7 +15,7 @@ VPATH = src SRC = $(notdir $(wildcard src/*.cpp)) OBJ = $(SRC:.cpp=.o) CC = c++ -CFLAGS = -Wall -Wextra -Werror -std=c++23 -MMD +CFLAGS = -Wall -Wextra -Werror -std=c++11 -MMD all: $(NAME) diff --git a/ex00/src/main.cpp b/ex00/src/main.cpp index 1d65b29..bda548d 100644 --- a/ex00/src/main.cpp +++ b/ex00/src/main.cpp @@ -8,25 +8,25 @@ int main() std::vector vec = {1, 2, 3, 4, 5}; try { - std::cout << "Testing with vector...\n"; + std::cout << "Testing with vector..." << std::endl; std::cout << "Vector contents: "; for (const auto &val : vec) { std::cout << val << " "; } - std::cout << "\n"; + std::cout << std::endl; - std::cout << "Searching for value 3...\n"; + std::cout << "Searching for value 3..." << std::endl; int &found = easyfind(vec, 3); std::cout << "Found: " << found << std::endl; - std::cout << "Modifying found value...\n"; + std::cout << "Modifying found value..." << std::endl; found = 10; // Modify the found value for (const auto &val : vec) { std::cout << val << " "; } - std::cout << "\n"; + std::cout << std::endl; } catch (const std::out_of_range &e) { @@ -35,7 +35,7 @@ int main() try { - std::cout << "Searching for value 6 (not in vector)...\n"; + std::cout << "Searching for value 6 (not in vector)..." << std::endl; int ¬Found = easyfind(vec, 6); std::cout << "Found: " << notFound << std::endl; } @@ -44,7 +44,7 @@ int main() std::cerr << e.what() << std::endl; } - std::cout << "\n\nTesting with deque...\n"; + std::cout << std::endl << std::endl << "Testing with deque..." << std::endl; std::deque deq = {10, 20, 30, 40, 50}; try @@ -54,17 +54,17 @@ int main() { std::cout << val << " "; } - std::cout << "\n"; - std::cout << "Searching for value 30...\n"; + std::cout << std::endl; + std::cout << "Searching for value 30..." << std::endl; int &foundInDeque = easyfind(deq, 30); - std::cout << "Found: " << foundInDeque << "\n"; - std::cout << "Modifying found value...\n"; + std::cout << "Found: " << foundInDeque << std::endl; + std::cout << "Modifying found value..." << std::endl; foundInDeque = 100; // Modify the found value for (const auto &val : deq) { std::cout << val << " "; } - std::cout << "\n"; + std::cout << std::endl; } catch (const std::out_of_range &e) { @@ -73,7 +73,7 @@ int main() try { - std::cout << "Searching for value 60 (not in deque)...\n"; + std::cout << "Searching for value 60 (not in deque)..." << std::endl; int ¬FoundInDeque = easyfind(deq, 60); std::cout << "Found: " << notFoundInDeque << std::endl; } diff --git a/ex01/inc/Span.hpp b/ex01/inc/Span.hpp index 54434f1..7d11690 100644 --- a/ex01/inc/Span.hpp +++ b/ex01/inc/Span.hpp @@ -1,6 +1,5 @@ #pragma once -#include #include #include #include @@ -13,7 +12,7 @@ class Span Span(const Span &other); Span(Span &&other) noexcept; Span &operator=(const Span &other); - Span &&operator=(Span &&other) noexcept; + Span &operator=(Span &&other) noexcept; ~Span() noexcept; void addNumber(int number); @@ -21,11 +20,10 @@ class Span unsigned int shortestSpan() const; unsigned int longestSpan() const; - template - requires std::same_as, int> + template void addRange(Iterator begin, Iterator end) { - for (Iterator it = begin; it < end; it++) + for (Iterator it = begin; it != end; ++it) { addNumber(*it); } diff --git a/ex01/src/Span.cpp b/ex01/src/Span.cpp index 9f6e9df..e723b91 100644 --- a/ex01/src/Span.cpp +++ b/ex01/src/Span.cpp @@ -8,13 +8,8 @@ Span::Span(unsigned int size) : _size(size), _data() { _data.reserve(size); } -Span::Span(const Span &other) : _size(other._size), _data(other._size) -{ - for (int n : other._data) - { - addNumber(n); - } -} +Span::Span(const Span &other) : _size(other._size), _data(other._data) +{} Span::Span(Span &&other) noexcept : _size(other._size), _data(std::move(other._data)) { @@ -31,7 +26,7 @@ Span &Span::operator=(const Span &other) return *this; } -Span &&Span::operator=(Span &&other) noexcept +Span &Span::operator=(Span &&other) noexcept { if (this != &other) { @@ -39,7 +34,7 @@ Span &&Span::operator=(Span &&other) noexcept _data = std::move(other._data); other._size = 0; } - return std::move(*this); + return *this; } Span::~Span() noexcept{} @@ -63,10 +58,11 @@ unsigned int Span::shortestSpan() const std::vector sortedData = _data; std::sort(sortedData.begin(), sortedData.end()); - unsigned int minSpan = std::numeric_limits::max(); + unsigned int minSpan = std::numeric_limits::max(); for (size_t i = 0; i < sortedData.size() - 1; i++) { - unsigned int span = sortedData[i + 1] - sortedData[i]; + unsigned int span = static_cast( + static_cast(sortedData[i + 1]) - static_cast(sortedData[i])); if (span < minSpan) { minSpan = span; @@ -86,7 +82,7 @@ unsigned int Span::longestSpan() const auto min = std::min_element(_data.begin(), _data.end()); auto max = std::max_element(_data.begin(), _data.end()); - return *max - *min; + return static_cast(static_cast(*max) - static_cast(*min)); } const char *Span::OutOfSpaceException::what() const throw() diff --git a/ex01/src/main.cpp b/ex01/src/main.cpp index 8f9b511..338817c 100644 --- a/ex01/src/main.cpp +++ b/ex01/src/main.cpp @@ -3,7 +3,6 @@ #include #include #include -#include #define GUB "\033[1;32;4m" #define ERROR "\033[1;31m" @@ -24,12 +23,12 @@ std::vector generateRandomVecor(int size) int main() { - std::println(GUB "Testing Span with more numbers then the size of the span..." END); + std::cout << GUB "Testing Span with more numbers then the size of the span..." END << std::endl; try { - std::println("Creating Span with size 5..."); + std::cout << "Creating Span with size 5..." << std::endl; Span span(5); - std::println("Adding 6 numbers to the span..."); + std::cout << "Adding 6 numbers to the span..." << std::endl; span.addNumber(1); span.addNumber(2); span.addNumber(3); @@ -39,92 +38,92 @@ int main() } catch (const Span::OutOfSpaceException &e) { - std::println(std::cerr, ERROR "Error: {}" END, e.what()); + std::cerr << ERROR "Error: " << e.what() << END << std::endl; } - std::println(); + std::cout << std::endl; - std::println(GUB "Testing Span with 0 numbers..." END); + std::cout << GUB "Testing Span with 0 numbers..." END << std::endl; try { - std::println("Creating Span with size 0..."); + std::cout << "Creating Span with size 0..." << std::endl; Span span(0); - std::println("Trying to find the shortest span..."); + std::cout << "Trying to find the shortest span..." << std::endl; span.shortestSpan(); // This should throw an exception } catch (const Span::NoSpanFoundException &e) { - std::println(std::cerr, ERROR "Error: {}" END, e.what()); + std::cerr << ERROR "Error: " << e.what() << END << std::endl; } - std::println(); + std::cout << std::endl; - std::println(GUB "Testing Span with one number..." END); + std::cout << GUB "Testing Span with one number..." END << std::endl; try { - std::println("Creating Span with size 1..."); + std::cout << "Creating Span with size 1..." << std::endl; Span span(1); - std::println("Adding 1 number to the span..."); + std::cout << "Adding 1 number to the span..." << std::endl; span.addNumber(42); - std::println("Trying to find the shortest span..."); + std::cout << "Trying to find the shortest span..." << std::endl; span.shortestSpan(); // This should throw an exception } catch (const Span::NoSpanFoundException &e) { - std::println(std::cerr, ERROR "Error: {}" END, e.what()); + std::cerr << ERROR "Error: " << e.what() << END << std::endl; } - std::println(); + std::cout << std::endl; - std::println(GUB "Testing Span with two numbers..." END); + std::cout << GUB "Testing Span with two numbers..." END << std::endl; try { - std::println("Creating Span with size 2..."); + std::cout << "Creating Span with size 2..." << std::endl; Span span(2); - std::println("Adding 2 numbers to the span..."); + std::cout << "Adding 2 numbers to the span..." << std::endl; span.addNumber(10); span.addNumber(20); - std::println("Trying to find the shortest span..."); + std::cout << "Trying to find the shortest span..." << std::endl; unsigned int shortest = span.shortestSpan(); - std::println("Shortest span: {}", shortest); + std::cout << "Shortest span: " << shortest << std::endl; - std::println("Finding the longest span..."); + std::cout << "Finding the longest span..." << std::endl; unsigned int longest = span.longestSpan(); - std::println("Longest span: {}", longest); + std::cout << "Longest span: " << longest << std::endl; } catch (const Span::NoSpanFoundException &e) { - std::println(std::cerr, ERROR "Error: {}" END, e.what()); + std::cerr << ERROR "Error: " << e.what() << END << std::endl; } catch (const Span::OutOfSpaceException &e) { - std::println(std::cerr, ERROR "Error: {}" END, e.what()); + std::cerr << ERROR "Error: " << e.what() << END << std::endl; } - std::println(); + std::cout << std::endl; - std::println(GUB "Testing Span with a range of random numbers..." END); + std::cout << GUB "Testing Span with a range of random numbers..." END << std::endl; try { - unsigned int size = 10'000; - std::println("Generating a vector with {} random numbers...", size); + unsigned int size = 10000; + std::cout << "Generating a vector with " << size << " random numbers..." << std::endl; std::vector randomNumbers = generateRandomVecor(size); - std::println("Adding the random numbers to a span of size {}...", size); + std::cout << "Adding the random numbers to a span of size " << size << "..." << std::endl; Span span(size); span.addRange(randomNumbers.begin(), randomNumbers.end()); - std::println("Finding the shortest span..."); + std::cout << "Finding the shortest span..." << std::endl; unsigned int shortest = span.shortestSpan(); - std::println("Shortest span: {}", shortest); + std::cout << "Shortest span: " << shortest << std::endl; - std::println("Finding the longest span..."); + std::cout << "Finding the longest span..." << std::endl; unsigned int longest = span.longestSpan(); - std::println("Longest span: {}", longest); + std::cout << "Longest span: " << longest << std::endl; } catch (const Span::OutOfSpaceException &e) { - std::println(std::cerr, ERROR "Error: {}" END, e.what()); + std::cerr << ERROR "Error: " << e.what() << END << std::endl; } catch (const Span::NoSpanFoundException &e) { - std::println(std::cerr, ERROR "Error: {}" END, e.what()); + std::cerr << ERROR "Error: " << e.what() << END << std::endl; } return 0; diff --git a/ex02/inc/MutantStack.hpp b/ex02/inc/MutantStack.hpp index 6bfc73c..2a2fbe8 100644 --- a/ex02/inc/MutantStack.hpp +++ b/ex02/inc/MutantStack.hpp @@ -9,7 +9,7 @@ class MutantStack : public std::stack MutantStack(const MutantStack &other); MutantStack(MutantStack &&other) noexcept; MutantStack &operator=(const MutantStack &other); - MutantStack &&operator=(MutantStack &&other) noexcept; + MutantStack &operator=(MutantStack &&other) noexcept; ~MutantStack() noexcept; typedef typename std::stack::container_type::iterator iterator; diff --git a/ex02/inc/MutantStack.tpp b/ex02/inc/MutantStack.tpp index 160ca76..9bb15dc 100644 --- a/ex02/inc/MutantStack.tpp +++ b/ex02/inc/MutantStack.tpp @@ -25,13 +25,13 @@ MutantStack &MutantStack::operator=(const MutantStack &other) } template -MutantStack &&MutantStack::operator=(MutantStack &&other) noexcept +MutantStack &MutantStack::operator=(MutantStack &&other) noexcept { if (this != &other) { std::stack::operator=(std::move(other)); } - return std::move(*this); + return *this; } template diff --git a/ex02/src/main.cpp b/ex02/src/main.cpp index 1b8170c..e42e384 100644 --- a/ex02/src/main.cpp +++ b/ex02/src/main.cpp @@ -1,6 +1,6 @@ #include "MutantStack.hpp" -#include +#include #define INFO "\n\033[1;32;4m" #define ERROR "\033[1;31m" @@ -8,71 +8,70 @@ int main() { - std::println(INFO "Testing MutantStack..." END); - std::println(INFO "Creating a MutantStack of integers..." END); + std::cout << INFO "Testing MutantStack..." END << std::endl; + std::cout << INFO "Creating a MutantStack of integers..." END << std::endl; MutantStack mutantStack; mutantStack.push(1); mutantStack.push(2); mutantStack.push(3); - std::println("MutantStack created with elements: 1, 2, 3"); - std::println("The size of MutantStack is: {}", mutantStack.size()); - std::println("Pop the top element: {}", mutantStack.top()); + std::cout << "MutantStack created with elements: 1, 2, 3" << std::endl; + std::cout << "The size of MutantStack is: " << mutantStack.size() << std::endl; + std::cout << "Pop the top element: " << mutantStack.top() << std::endl; mutantStack.pop(); - std::println("The size of MutantStack after pop is: {}", mutantStack.size()); + std::cout << "The size of MutantStack after pop is: " << mutantStack.size() << std::endl; - std::println(INFO "Testing Copy constructor..." END); + std::cout << INFO "Testing Copy constructor..." END << std::endl; MutantStack copyStack(mutantStack); - std::println("Copy constructor called."); - std::println("The size of the copied MutantStack is: {}", copyStack.size()); - std::println("The elements of the copied MutantStack are: "); + std::cout << "Copy constructor called." << std::endl; + std::cout << "The size of the copied MutantStack is: " << copyStack.size() << std::endl; + std::cout << "The elements of the copied MutantStack are: " << std::endl; for (MutantStack::iterator it = copyStack.begin(); it != copyStack.end(); ++it) { - std::println(" - {}", *it); + std::cout << " - " << *it << std::endl; } - std::println(INFO "Testing the Copy assignment operator..." END); + std::cout << INFO "Testing the Copy assignment operator..." END << std::endl; MutantStack assignStack; assignStack = copyStack; - std::println("Copy assignment operator called."); - std::println("The size of the assigned MutantStack is: {}", assignStack.size()); - std::println("The elements of the assigned MutantStack are: "); + std::cout << "Copy assignment operator called." << std::endl; + std::cout << "The size of the assigned MutantStack is: " << assignStack.size() << std::endl; + std::cout << "The elements of the assigned MutantStack are: " << std::endl; for (MutantStack::iterator it = assignStack.begin(); it != assignStack.end(); ++it) { - std::println(" - {}", *it); + std::cout << " - " << *it << std::endl; } - std::println(INFO "Testing Move constructor..." END); + std::cout << INFO "Testing Move constructor..." END << std::endl; MutantStack moveStack(std::move(mutantStack)); - std::println("Move constructor called."); - std::println("The size of the moved MutantStack is: {}", moveStack.size()); - std::println("The elements of the moved MutantStack are: "); + std::cout << "Move constructor called." << std::endl; + std::cout << "The size of the moved MutantStack is: " << moveStack.size() << std::endl; + std::cout << "The elements of the moved MutantStack are: " << std::endl; for (MutantStack::iterator it = moveStack.begin(); it != moveStack.end(); ++it) { - std::println(" - {}", *it); + std::cout << " - " << *it << std::endl; } - std::println(INFO "Testing Move assignment operator..." END); + std::cout << INFO "Testing Move assignment operator..." END << std::endl; MutantStack moveAssignStack; moveAssignStack = std::move(moveStack); - std::println("Move assignment operator called."); - std::println("The size of the assigned MutantStack is: {}", moveAssignStack.size()); - std::println("The elements of the assigned MutantStack are: "); + std::cout << "Move assignment operator called." << std::endl; + std::cout << "The size of the assigned MutantStack is: " << moveAssignStack.size() << std::endl; + std::cout << "The elements of the assigned MutantStack are: " << std::endl; for (MutantStack::iterator it = moveAssignStack.begin(); it != moveAssignStack.end(); ++it) { - std::println(" - {}", *it); + std::cout << " - " << *it << std::endl; } - std::println(INFO "Testing MutantStack with const iterators..." END); - MutantStack constStack; - constStack.push(1); - constStack.push(2); - constStack.push(3); - std::println("Const MutantStack created with elements: 1, 2, 3"); - std::println("The size of Const MutantStack is: {}", constStack.size()); - std::println("Pop the top element: {}", constStack.top()); - constStack.pop(); - std::println("The size of Const MutantStack after pop is: {}", constStack.size()); - std::println("The elements of the Const MutantStack are: "); + std::cout << INFO "Testing MutantStack with const iterators..." END << std::endl; + MutantStack stack; + stack.push(1); + stack.push(2); + stack.push(3); + const MutantStack constStack(stack); + std::cout << "Const MutantStack created with elements: 1, 2, 3" << std::endl; + std::cout << "The size of Const MutantStack is: " << constStack.size() << std::endl; + std::cout << "Top element of Const MutantStack: " << constStack.top() << std::endl; + std::cout << "The elements of the Const MutantStack are: " << std::endl; for (MutantStack::const_iterator it = constStack.begin(); it != constStack.end(); ++it) { - std::println(" - {}", *it); + std::cout << " - " << *it << std::endl; } }