last fixes and rewrite to cpp11
This commit is contained in:
parent
35286da232
commit
8744763d48
@ -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)
|
||||
|
||||
|
||||
@ -8,25 +8,25 @@ int main()
|
||||
std::vector<int> 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<int> 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;
|
||||
}
|
||||
|
||||
@ -1,6 +1,5 @@
|
||||
#pragma once
|
||||
|
||||
#include <concepts>
|
||||
#include <exception>
|
||||
#include <iterator>
|
||||
#include <vector>
|
||||
@ -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 <std::input_iterator Iterator>
|
||||
requires std::same_as<std::iter_value_t<Iterator>, int>
|
||||
template <typename Iterator>
|
||||
void addRange(Iterator begin, Iterator end)
|
||||
{
|
||||
for (Iterator it = begin; it < end; it++)
|
||||
for (Iterator it = begin; it != end; ++it)
|
||||
{
|
||||
addNumber(*it);
|
||||
}
|
||||
|
||||
@ -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<int> sortedData = _data;
|
||||
std::sort(sortedData.begin(), sortedData.end());
|
||||
|
||||
unsigned int minSpan = std::numeric_limits<int>::max();
|
||||
unsigned int minSpan = std::numeric_limits<unsigned int>::max();
|
||||
for (size_t i = 0; i < sortedData.size() - 1; i++)
|
||||
{
|
||||
unsigned int span = sortedData[i + 1] - sortedData[i];
|
||||
unsigned int span = static_cast<unsigned int>(
|
||||
static_cast<long long>(sortedData[i + 1]) - static_cast<long long>(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<unsigned int>(static_cast<long long>(*max) - static_cast<long long>(*min));
|
||||
}
|
||||
|
||||
const char *Span::OutOfSpaceException::what() const throw()
|
||||
|
||||
@ -3,7 +3,6 @@
|
||||
#include <cstdlib>
|
||||
#include <ctime>
|
||||
#include <iostream>
|
||||
#include <print>
|
||||
|
||||
#define GUB "\033[1;32;4m"
|
||||
#define ERROR "\033[1;31m"
|
||||
@ -24,12 +23,12 @@ std::vector<int> 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<int> 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;
|
||||
|
||||
@ -9,7 +9,7 @@ class MutantStack : public std::stack<T>
|
||||
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<T>::container_type::iterator iterator;
|
||||
|
||||
@ -25,13 +25,13 @@ MutantStack<T> &MutantStack<T>::operator=(const MutantStack &other)
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
MutantStack<T> &&MutantStack<T>::operator=(MutantStack &&other) noexcept
|
||||
MutantStack<T> &MutantStack<T>::operator=(MutantStack &&other) noexcept
|
||||
{
|
||||
if (this != &other)
|
||||
{
|
||||
std::stack<T>::operator=(std::move(other));
|
||||
}
|
||||
return std::move(*this);
|
||||
return *this;
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
|
||||
@ -1,6 +1,6 @@
|
||||
#include "MutantStack.hpp"
|
||||
|
||||
#include <print>
|
||||
#include <iostream>
|
||||
|
||||
#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<int> 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<int> 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<int>::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<int> 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<int>::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<int> 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<int>::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<int> 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<int>::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<int> 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<int> stack;
|
||||
stack.push(1);
|
||||
stack.push(2);
|
||||
stack.push(3);
|
||||
const MutantStack<int> 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<int>::const_iterator it = constStack.begin(); it != constStack.end(); ++it)
|
||||
{
|
||||
std::println(" - {}", *it);
|
||||
std::cout << " - " << *it << std::endl;
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user