What you think about throwing an exception for not found in C++?
c++, containers, exception, usagepatterns
Solution
The STL deals with this situation by using iterators. For example, the std::map class has a similar function:
iterator find( const key_type& key );
If the key isn't found, it returns 'end()'. You may want to use this iterator approach, or to use some sort of wrapper for your return value.
Problem
I know most people think that as a bad practice but when you are trying to make your class public interface only work with references, keeping pointers inside and only when necessary, I think there is no way to return something telling that the value you are looking doesn't exist in the container. ``` class list { public: value &get(type key); }; ``` Let's think that you don't want to have dangerous pointers being saw in the public interface of the class, how do you return a not found in this case, throwing an exception? What is your approach to that? Do you return an empty value and check for the empty state of it? I actually use the throw approach but I introduce a checking method: ``` class list { public: bool exists(type key); value &get(type key); }; ``` So when I forget to check that the value exists first I get an exception, that is really an exception. How would you do it?