   Returning by value from datastructure
  Hey.Lets assume language C++.We have a generic datastructure Tree< T, R >, that accepts any kind of data for its leaves ( primitives, objects, structs, pointers... ) and accepts any primitive as key ( float, int, long, bool... )The datastructure would implement two public methods:void insert( T payLoad, R key );Tfetch( R key );This would seem to be a nice solution, but what the heck are you supposed to do if you try and query the datastructure for key that has no payload associated with it?  The function prototype promises a return value, and even that is by value, so just throwing NULL is not an option. For generic Stacks and Queues, it would simply be a matter of "peeking" whether the thing was empty, but with a Tree, you would be forced to do something like if(Exists(R)){ T t = Fetch(R);}Traversing the tree twice for every query is not exactly delightful though for me.Alternatively I could throw an exception on "not found" event, but that be mixing exception handling code with regular program flow, so thats a no-go too.Another solution I have thought would involve setting up a dummy object on stack along with a boolean variable. On call, pointers to both would be passed to the Tree, and on succesful query, the bool would be set to true through the pointer to signal meaningful data content on the other variable. Naturally, this won't be too elegant or even possible with complex class hierarchies.I would hate to be forced to return pointers, since the generic nature of the datastructure would suffer. ( For example: pointer to byte is just waste in this context ).   
  Use an iterator, same as std::map does?typedef std::map<int, std::string> m_t;m_t m;m_t::iterator i = m.find(0);if (i != m.end()) {    // do whatever}Edit: You might want to look at adobe forest which is quite a nice tree implementation.   ;Quote:Original post by rmed002Lets assume language C++.We have a generic datastructure Tree< T, R >, that accepts any kind of data for its leaves ( primitives, objects, structs, pointers... ) and accepts any primitive as key ( float, int, long, bool... )Sounds like std::map. All your other questions are answered there.   ;  It does seem like I have little choice in the matter. I was hoping to bypass the need for "additional" components and deal with the entries directly. Apparently its not that easy or practical afterall.Then again, In any serious application I would be better off using STL anyways, so my little excercise is becoming more and more pointless by the minute. ^^Curious regardless and thanks for help. :);Quote:Original post by rmed002It does seem like I have little choice in the matter. I was hoping to bypass the need for "additional" components and deal with the entries directly. Apparently its not that easy or practical afterall.See std::map::operator[] !   ;Quote:Original post by the_eddQuote:Original post by rmed002It does seem like I have little choice in the matter. I was hoping to bypass the need for "additional" components and deal with the entries directly. Apparently its not that easy or practical afterall.See std::map::operator[] !I wish std::map provided operator[]! rather than plain old operator [] [grin]   