From fd0bfc542092ae2b238169f586418cfcc5bbe31b Mon Sep 17 00:00:00 2001 From: yosyo Date: Mon, 25 Aug 2025 12:08:16 +0200 Subject: [PATCH] =?UTF-8?q?=E3=80=8C=E2=9C=A8=E3=80=8D=20feat:=20ex00=20:D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ex00/Makefile | 58 ++++++++++++++++++++++++++++++++++++++++ ex00/easyfind.hpp | 11 ++++++++ ex00/main.cpp | 68 +++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 137 insertions(+) create mode 100644 ex00/Makefile create mode 100644 ex00/easyfind.hpp create mode 100644 ex00/main.cpp diff --git a/ex00/Makefile b/ex00/Makefile new file mode 100644 index 0000000..b0da4ad --- /dev/null +++ b/ex00/Makefile @@ -0,0 +1,58 @@ +# **************************************************************************** # +# # +# ::: :::::::: # +# Makefile :+: :+: :+: # +# +:+ +:+ +:+ # +# By: mmoussou + +template +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; +} diff --git a/ex00/main.cpp b/ex00/main.cpp new file mode 100644 index 0000000..510fd31 --- /dev/null +++ b/ex00/main.cpp @@ -0,0 +1,68 @@ +#include +#include +#include + +#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 vec(arr, arr + size); + + try + { + std::cout << "trying to find 21.." << std::endl; + std::vector::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::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 l(arr, arr + size); + + try + { + std::cout << "trying to find 21.." << std::endl; + std::list::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::iterator r = easyfind(l, -5); + std::cout << "easyfind result: " << *r << std::endl; + } + catch (std::runtime_error &e) + { + std::cerr << e.what() << std::endl; + } + } +}