From 4115f22c50ee0571e88a8ec44c0241693af7ed38 Mon Sep 17 00:00:00 2001 From: Charles Cabergs Date: Tue, 15 Dec 2020 14:25:57 +0100 Subject: Fixing cpp08/{00,01} --- cpp08/ex00/easyfind.hpp | 9 ++++++--- cpp08/ex00/main.cpp | 29 +++++++++++------------------ 2 files changed, 17 insertions(+), 21 deletions(-) (limited to 'cpp08/ex00') diff --git a/cpp08/ex00/easyfind.hpp b/cpp08/ex00/easyfind.hpp index 68e176c..7523f53 100644 --- a/cpp08/ex00/easyfind.hpp +++ b/cpp08/ex00/easyfind.hpp @@ -6,7 +6,7 @@ /* By: charles +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/04/15 05:53:04 by charles #+# #+# */ -/* Updated: 2020/04/15 06:47:27 by charles ### ########.fr */ +/* Updated: 2020/12/15 11:58:54 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ @@ -16,9 +16,12 @@ # include template -typename T::iterator easyfind(T& container, int x) +int& easyfind(T& container, int x) { - return std::find(container.begin(), container.end(), x); + typename T::iterator found = std::find(container.begin(), container.end(), x); + if (found == container.end()) + throw std::exception(); + return *found; } #endif diff --git a/cpp08/ex00/main.cpp b/cpp08/ex00/main.cpp index 8824f99..af8f81c 100644 --- a/cpp08/ex00/main.cpp +++ b/cpp08/ex00/main.cpp @@ -6,7 +6,7 @@ /* By: charles +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/04/15 05:57:10 by charles #+# #+# */ -/* Updated: 2020/04/15 06:47:52 by charles ### ########.fr */ +/* Updated: 2020/12/15 11:59:54 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ @@ -23,24 +23,17 @@ int main() std::list c(a, a + 6); std::deque d(a, a + 6); - std::cout << (easyfind(b, 10) != b.end()) << std::endl; - std::cout << (easyfind(c, 20) != c.end()) << std::endl; - std::cout << (easyfind(d, 30) != d.end()) << std::endl; - std::cout << (easyfind(b, 40) != b.end()) << std::endl; - std::cout << (easyfind(c, 50) != c.end()) << std::endl; - std::cout << (easyfind(d, 60) != d.end()) << std::endl; + std::cout << easyfind(b, 10) << std::endl; + std::cout << easyfind(c, 20) << std::endl; + std::cout << easyfind(d, 30) << std::endl; + std::cout << easyfind(b, 40) << std::endl; + std::cout << easyfind(c, 50) << std::endl; + std::cout << easyfind(d, 60) << std::endl; - std::cout << *easyfind(b, 10) << std::endl; - std::cout << *easyfind(c, 20) << std::endl; - std::cout << *easyfind(d, 30) << std::endl; - std::cout << *easyfind(b, 40) << std::endl; - std::cout << *easyfind(c, 50) << std::endl; - std::cout << *easyfind(d, 60) << std::endl; - - - std::cout << (easyfind(b, 70) != b.end()) << std::endl; - std::cout << (easyfind(c, 80) != c.end()) << std::endl; - std::cout << (easyfind(d, 90) != d.end()) << std::endl; + try { std::cout << easyfind(b, 70) << std::endl; } catch (std::exception &e) { std::cout << e.what() << std::endl; } + try { std::cout << easyfind(c, 80) << std::endl; } catch (std::exception &e) { std::cout << e.what() << std::endl; } + try { std::cout << easyfind(d, 90) << std::endl; } catch (std::exception &e) { std::cout << e.what() << std::endl; } + try { std::cout << easyfind(d, -1) << std::endl; } catch (std::exception &e) { std::cout << e.what() << std::endl; } return 0; } -- cgit