aboutsummaryrefslogtreecommitdiff
path: root/cpp08/ex00
diff options
context:
space:
mode:
authorCharles Cabergs <me@cacharle.xyz>2020-12-15 14:25:57 +0100
committerCharles Cabergs <me@cacharle.xyz>2020-12-15 14:25:57 +0100
commit4115f22c50ee0571e88a8ec44c0241693af7ed38 (patch)
tree9e702e34ba360aa5f10f8f84b37b97b6ca8dd731 /cpp08/ex00
parent78efa932fb42289904fe542cfc152978397ae37c (diff)
downloadpiscine_cpp-4115f22c50ee0571e88a8ec44c0241693af7ed38.tar.gz
piscine_cpp-4115f22c50ee0571e88a8ec44c0241693af7ed38.tar.bz2
piscine_cpp-4115f22c50ee0571e88a8ec44c0241693af7ed38.zip
Fixing cpp08/{00,01}
Diffstat (limited to 'cpp08/ex00')
-rw-r--r--cpp08/ex00/easyfind.hpp9
-rw-r--r--cpp08/ex00/main.cpp29
2 files changed, 17 insertions, 21 deletions
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 <charles.cabergs@gmail.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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 <algorithm>
template<typename T>
-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 <charles.cabergs@gmail.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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<int> c(a, a + 6);
std::deque<int> 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;
}