」 feat: ex00 :D

This commit is contained in:
2025-08-25 12:08:16 +02:00
commit fd0bfc5420
3 changed files with 137 additions and 0 deletions

58
ex00/Makefile Normal file
View File

@@ -0,0 +1,58 @@
# **************************************************************************** #
# #
# ::: :::::::: #
# Makefile :+: :+: :+: #
# +:+ +:+ +:+ #
# By: mmoussou <mmoussou@student.42angouleme.fr +#+ +:+ +#+ #
# +#+#+#+#+#+ +#+ #
# Created: 2024/01/22 07:21:18 by mmoussou #+# #+# #
# Updated: 2025/08/25 11:08:56 by mmoussou ### ########.fr #
# #
# **************************************************************************** #
SHELL = bash
RED = \033[0;31m
GREEN = \033[0;32m
YELLOW = \033[1;33m
PURPLE = \e[0;35m
NC = \033[0m
DELETE = \x1B[2K
CC = c++
FLAGS = -std=c++98 -Wall -Werror -Wextra
INCLUDES = .
NAME = ex00
SRCS = main.cpp
OBJSDIR = obj/
OBJS = $(addprefix $(OBJSDIR), $(SRCS:.cpp=.o))
all: $(NAME)
$(NAME): $(OBJS)
@$(CC) $(FLAGS) -I$(INCLUDES) $(OBJS) -o $(NAME)
@printf "$(NC)$(YELLOW)「✨」 feat($(NAME)): program compiled\n$(NC)"
$(OBJSDIR)%.o: %.cpp
@mkdir -p $(@D)
@$(CC) $(FLAGS) -I$(INCLUDES) -c $< -o $@
@printf "$(NC)$(GREEN)「🔨」 build($<): object compiled\n$(NC)"
clean:
@rm -f $(OBJS)
@printf "$(NC)$(RED)「🗑️」 clean($(OBJS)): object deleted\n$(NC)"
fclean: clean
@rm -f $(NAME)
@rm -Rf $(OBJSDIR)
@printf "$(NC)$(RED)「🗑️」 fclean($(NAME)): program deleted\n$(NC)"
re: fclean
@$(MAKE) -s all
.PHONY: clean fclean all re

11
ex00/easyfind.hpp Normal file
View File

@@ -0,0 +1,11 @@
#include <algorithm>
template <typename T>
typename T::iterator easyfind(T &value, int what)
{
typename T::iterator it = std::find(value.begin(), value.end(), what);
if (it == value.end())
throw std::runtime_error("value not found");
return it;
}

68
ex00/main.cpp Normal file
View File

@@ -0,0 +1,68 @@
#include <list>
#include <vector>
#include <iostream>
#include "easyfind.hpp"
int main(void)
{
int arr[] = { 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377 };
int size = sizeof(arr) / sizeof(arr[0]);
// vector
{
std::cout << "\033[33mVectors:\033[0m" << std::endl;
std::vector<int> vec(arr, arr + size);
try
{
std::cout << "trying to find 21.." << std::endl;
std::vector<int>::iterator r = easyfind(vec, 21);
std::cout << "easyfind result: " << *r << "(" << r - vec.begin() << ")" << std::endl;
}
catch (std::runtime_error &e)
{
std::cerr << e.what() << std::endl;
}
std::cout << std::endl;
try
{
std::cout << "trying to find a non-existent value (100)" << std::endl;
std::vector<int>::iterator r = easyfind(vec, 100);
std::cout << "easyfind result: " << *r << std::endl;
}
catch (std::runtime_error &e)
{
std::cerr << e.what() << std::endl;
}
}
std::cout << std::endl;
// list
{
std::cout << "\033[33mLists:\033[0m" << std::endl;
std::list<int> l(arr, arr + size);
try
{
std::cout << "trying to find 21.." << std::endl;
std::list<int>::iterator r = easyfind(l, 21);
std::cout << "easyfind result: " << *r << "(" << std::distance(l.begin(), r) << ")" << std::endl;
}
catch (std::runtime_error &e)
{
std::cerr << e.what() << std::endl;
}
std::cout << std::endl;
try
{
std::cout << "trying to find a non-existent value (-5)" << std::endl;
std::list<int>::iterator r = easyfind(l, -5);
std::cout << "easyfind result: " << *r << std::endl;
}
catch (std::runtime_error &e)
{
std::cerr << e.what() << std::endl;
}
}
}