1
0

」 feat: finished ex00

This commit is contained in:
2025-04-14 19:39:20 +02:00
commit 35fc155f65
8 changed files with 246 additions and 0 deletions

60
ex00/Makefile Normal file
View File

@ -0,0 +1,60 @@
# **************************************************************************** #
# #
# ::: :::::::: #
# Makefile :+: :+: :+: #
# +:+ +:+ +:+ #
# By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ #
# +#+#+#+#+#+ +#+ #
# Created: 2024/10/25 16:09:27 by adjoly #+# #+# #
# Updated: 2025/04/14 19:06:23 by adjoly ### ########.fr #
# #
# **************************************************************************** #
SHELL = bash
NAME = easyfind
CC = c++
OBJSDIR = obj/
SRCS = $(shell find . -name '*.cpp')
OBJS = $(addprefix $(OBJSDIR), $(SRCS:.cpp=.o))
FLAGS = -Wall -Werror -Wextra -std=c++98 -MMD -MP
RED = \033[0;31m
GREEN = \033[0;32m
YELLOW = \033[1;33m
PURPLE = \e[0;35m
NC = \033[0m
DELETE = \x1B[2K\r
ifeq ($(VERBOSE),true)
FLAGS += -D VERBOSE
endif
all: $(NAME)
$(NAME): $(OBJS)
@$(CC) $(FLAGS) -I . $(OBJS) -o $(NAME)
@printf "$(YELLOW)「✨」($(NAME)) Program compiled\n"
$(OBJSDIR)%.o: %.cpp
@mkdir -p $(@D)
@$(CC) $(FLAGS) -I . -c $< -o $@
@printf "$(DELETE)$(GREEN)「🔨」($<) Object compiled\n"
clean:
@rm -f $(OBJS)
@printf "$(DELETE)$(RED)「🗑️」($(OBJS)) Object deleted\n"
fclean: clean
@rm -f $(NAME)
@rm -Rf $(OBJSDIR)
@printf "$(RED)「🗑️」($(NAME)) Program deleted\n"
re: fclean all
.PHONY: clean fclean all re

BIN
ex00/easyfind Executable file

Binary file not shown.

24
ex00/easyfind.hpp Normal file
View File

@ -0,0 +1,24 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* easyfind.hpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/04/14 19:07:43 by adjoly #+# #+# */
/* Updated: 2025/04/14 19:33:52 by adjoly ### ########.fr */
/* */
/* ************************************************************************** */
#pragma once
#include <algorithm>
#include <iterator>
#include <stdexcept>
template <typename T> typename T::iterator easyfind(T val, int what) {
typename T::iterator it = std::find(val.begin(), val.end(), what);
if (it == val.end())
throw std::runtime_error("value not found");
return it;
}

40
ex00/main.cpp Normal file
View File

@ -0,0 +1,40 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* main.cpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/04/14 19:33:56 by adjoly #+# #+# */
/* Updated: 2025/04/14 19:39:02 by adjoly ### ########.fr */
/* */
/* ************************************************************************** */
#include "easyfind.hpp"
#include <stdexcept>
#include <vector>
#include <iostream>
std::vector<int> *newVec() {
std::vector<int> *vec = new std::vector<int>;
for (size_t i = 0; i < 10; i++) {
vec->push_back(i);
}
return vec;
}
int main(void) {
std::vector<int> *vec = newVec();
try {
std::vector<int>::iterator it = easyfind<std::vector<int> >(*vec, 8);
std::cout << "found value : " << *it << std::endl;
} catch (std::runtime_error &e) {
std::cerr << e.what() << std::endl;
}
try {
std::vector<int>::iterator it = easyfind<std::vector<int> >(*vec, 13);
std::cout << "found value : " << *it << std::endl;
} catch (std::runtime_error &e) {
std::cerr << e.what() << std::endl;
}
}