Mainly it's a matter of code reuse, in the stl you can have algorithms that can be used on any stl container, or any other container for that matter. I'd suggest you this interesting read, by Stepanov, the father of the stl. The chore of defining ctor/dtor/copy constructors etc is left to the readerīe aware that this approach is against the "good practices" of C++, where usually we keep data separated from algorithms. #define CPPX_ITEMS_OF( c ) std::begin( c ), std::end( c )Īuto contains( Value& value, Container& container ) I would define some as-general-as-possible simple helper functions, e.g. Is there some reason that STL make the most common usages so wordy, and doesn’t overload to default on the whole container?) (Also, as an optional point which might shed some insight into this, I don’t understand why STL makes it so wordy and awkward to do common things like search a vector for an element, or sort a vector. In conclusion, my question this: what is the best way to add common, simple generic functions on vectors to a program? Option 3 is probably the cleanest, but has semantic issues that make it unclear. I don’t even know how to write a template function that intelligently decides whether to pass its argument by value or reference, and, even if I did, it sounds hard to do. Well, the thing is that we actually only want to pass in x by reference if the size of T is large, otherwise we want to use value. Consider writing a special function probe, like template probe(const vector & v, const T &x). Option 1 seems simple but gets very wordy after a while. Have a personal vector class K that subclasses both vector and a mixin class algorithm_vector with the algorithms I need. Write a template header for common vector functions Write special purpose code for each type of vector I use in the code. I assume that virtually every C++ programmer has run into this issue or issues like them. Getting unique values of v in STL is even more ridiculously clunky, requiring, I believe: std::erase(std::unique(std::sort(v.begin(),v.end()).end(),v.end()) (I.e., I cannot easily sort foo->bar.names without a temporary. Which makes std::sort very awkward to use in the case of complex lvalue expressions, requiring a temporary. For example, v.probe(x) would be something like std::find(v.begin(),v.end(),x)!=v.end()Īnd v.sort is std::sort(v.begin(),v.end()) These can all be done in stl, but the interface is clunky and wordy. V.locate(x) // pointer to the element in v equal to x if it exists, otherwise NULL Ideally I would like to make calls like: v.probe(x) //returns true if x is in v There are certain functions I use in manipulating vector that come up a lot but for which the standard interface is clunky.įor example, suppose v is if of type vector for a typename T.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |