D
Darshan
Hi everyone,
Can anyone suggest a good practice to learn and practice C++ STL?
Darshan
Can anyone suggest a good practice to learn and practice C++ STL?
Darshan
Darshan said:Can anyone suggest a good practice to learn and practice C++ STL?
Juha said:Impletent std::list from scratch. If after that you don't understand
STL inside and out, I don't know how you will.
(Ok, maybe it is a bit too laborious of an exercise. But I have
learned quite a lot by doing that.)
Impletent std::list from scratch. If after that you don't understand
STL inside and out, I don't know how you will.
(Ok, maybe it is a bit too laborious of an exercise. But I have
learned quite a lot by doing that.)
Brian said:Container-wise I suggest using Boost Intrusive containers
where possible -- http://www.boost.org/doc/libs/1_39_0/doc/html/intrusive.html.
I don't quite get what's so great about them. Especially this listed
downside seems to defeat the whole purpose of it being a data container
in the first place:
"The user has to manage the lifetime of inserted objects independently
from the containers."
The whole idea of a data container is to *store* data. In other words
the container takes care of managing the lifetimes of the stored data.
Now if you use a boost intrusive container, the container is not going
to manage the data at all, but you have to do it yourself. How? The only
possibility for managing dynamic data is to use a container such as the
ones offered by the standard C++ library. (How else are you going to
manage dynamic data? Doing direct news and deletes on the objects is
hazardous and certainly not recommended, especially not for someone who
is still learning C++ and STL.)
So it sounds like it would defeat the whole purpose. Why have the same
data in two different containers? What's the point?
Brian said:I don't follow you very well in the last two paragraphs,
but I don't need to store the same data in two different
containers.
The only alternative to storing your objects in a data container is to
"manually" allocate them with 'new' and destroy them with 'delete'
(while keeping the handles to them in the intrusive data container).
However, if you are going to manually allocate and destroy your objects,
you are begging for trouble unless you also use some garbage collection
engine. It's way too easy to make mistakes which will result in memory
leaks, double deletions or accessing deleted memory.
Since doing "raw" news and deletes is hazardous and asking for
trouble, it's safer to store the objects in a managed data container,
such as the ones offered by the STL. But if you do that, you are
defeating the whole purpose of using the boost intrusive containers.
Want to reply to this thread or ask your own question?
You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.