C++ is a multi-paradigm language and STL and Boost are built towards the functional paradigm of the language. STL is composed of containers (to hold data), iterators (to access data) and algorithms (functions to manipulate data). Algorithm functions are applied on containers by using iterators. As a side-effect, these methods are not part of the container classes, but are completely separate. (This avoids redundancy for the library writers, but is painful for library users.)
Are there C++ alternatives to STL/Boost which offer such containers in a more traditional object-oriented flavour? I am looking for strings, vectors, linked lists, map, trees, hash tables and such. Containers should be easy to inherit and extend. In comparison, extending classes from STL/Boost is a very bad idea and this is by design of their designers.
PS: Please do not use the reply space below to pontificate the advantages of STL/Boost. I am well aware of them! :-)
Ultimately, you should learn both. But STL can be learned in isolation, whereas boost won't make much sense until you understand the STL, since that's what Boost is modeled on, and designed to extend. So learn to use the STL as part of learning c++.
Introduction. Boost. Container library implements several well-known containers, including STL containers.
Boost provides free peer-reviewed portable C++ source libraries. We emphasize libraries that work well with the C++ Standard Library. Boost libraries are intended to be widely useful, and usable across a broad spectrum of applications.
Many (most!) older libraries for C++ used containers that bore a much closer resemblance to those used in such things as Java and C#.
A few example of such libraries include COOL, ET++, the NIH Class Library, and Rogue Wave Tools.h++.
Two points:
To be sure I'm clear here, at least IMO:
You're on your own. You have been warned!
Closed captioning for the humor impaired: of course some of that is meant to be humorous -- it is a really, really lousy idea though
This avoids redundancy for the library writers, but is painful for library users.
I don't agree with this premise at all. And even if I did, it's a huge over-generalization that doesn't apply to every library user. But this is a subjective statement anyway, so I'll ignore it.
Are there C++ alternatives to STL/Boost which offer such containers in a more traditional object-oriented flavour?
...
Containers should have methods that allow one to manipulate on them directly. (For example, calling vector.sort() instead of sort(vector.begin(),vector.end()).
Sure. Just make your own containers that have the standard containers as data members and delegate calls to them and to algorithms as needed via member functions. It's rather trivial to implement:
template<typename T>
class MyVector
{
public:
void sort()
{
std::sort(vec.begin(), vec.end());
}
// ...
private:
std::vector<T> vec;
};
There's nothing in C++ that prevents you from doing something like this, ironically thanks to the multi-paradigm nature of C++ that you seem to not agree with.
You can probably use private
inheritance and using
declarations if you much rather not write out wrapper functions.
STL/Boost make it a pain to derive from their containers and extend them.
That's because you're not supposed to derive from them. The proper way is to use composition, like the code snippet I presented above.
You're heading the wrong way. If you want to program in Java then program in Java. If you program in C++ then program as C++ programmers do. Always swim with the current, never against it.
STL and Boost are as object-oriented as you can get.
For all theoretical purposes, member function and a free function overloaded on the first argument are the same thing. They behave very similarly in practice, including for inheritance, so in C++ you should really consider free functions taking (possibly const) reference as first arguments to be methods of their first argument.
Advantage of free functions is they can be defined for existing class allowing you to add an interface to existing class. Which is why STL and especially boost uses them so much. Main advantage of member functions is they can be virtual (but virtual methods should be private anyway!)
You don't want to extend collections by derivation. Generally, you don't want to extend anything by derivation unless it is an abstract base class specifically designed for it. See this question about advantages of composition over inheritance.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With