easyfind.hpp
1 /* ************************************************************************** */ 2 /* */ 3 /* ::: :::::::: */ 4 /* easyfind.hpp :+: :+: :+: */ 5 /* +:+ +:+ +:+ */ 6 /* By: gychoi <gychoi@student.42seoul.kr> +#+ +:+ +#+ */ 7 /* +#+#+#+#+#+ +#+ */ 8 /* Created: 2023/12/25 22:52:29 by gychoi #+# #+# */ 9 /* Updated: 2023/12/30 20:59:46 by gychoi ### ########.fr */ 10 /* */ 11 /* ************************************************************************** */ 12 13 #pragma once 14 #ifndef __EASYFIND_HPP__ 15 #define __EASYFIND_HPP__ 16 17 #include <stdexcept> 18 #include <sstream> 19 20 template <typename T> 21 typename T::iterator easyfind(T& container, int const& value) 22 { 23 typename T::iterator it; 24 for (it = container.begin(); it != container.end(); it++) 25 { 26 if (*it == value) 27 { 28 return it; 29 } 30 } 31 32 std::ostringstream msg; 33 msg << "Value " << value << " not found in container"; 34 throw std::runtime_error(msg.str()); 35 } 36 37 template <typename T> 38 typename T::const_iterator easyfind(T const& container, int const& value) 39 { 40 typename T::const_iterator cit; 41 for (cit = container.begin(); cit != container.end(); cit++) 42 { 43 if (*cit == value) 44 { 45 return cit; 46 } 47 } 48 49 std::ostringstream msg; 50 msg << "Value " << value << " not found in container"; 51 throw std::runtime_error(msg.str()); 52 } 53 54 #endif /* __EASYFIND_HPP__ */