Did you want some of the map's in the subsequent code to be changed to set<T*>?no. in my case I had two keys for each object such that two maps were necessary. but didn't had the requirement to erase objects by key. that's why my "all"-container was a vector.
-- is v0 your v in the first code block?no. the v was replaced by set s in the second code block. a set s was needed to access the objects by pointer key in the first container.
-- is v0 your v in the first code block?
std::vector<T*> & v1 = f1->second;
i get a reference to the vector of all pointers for the key1. Storing raw pointers is a bad idea because it make cleaning up hard. The code becomes brittle, and it's too easy to end up leaking memory. The safer (and preferred) solution is to use a smart pointer.i like this statement but it is not true. any non-trivial enhancement of a container of the standard library necessarily would need to store (raw) pointers since c/c++ neither allows to store references nor to store different objects in one container. if you take for example a container which stores the objects read from a sql table and wants to provide an index of two of the columns the table has, the most simplest "container" to achieve that is a std::vector to store all the raw pointers and two std::map with <key, pointer> pair, one for each index. of course, the std::vector is the one who cares for cleaning at destruction time what isn't hard to accomplish and won't end in memory leaks. and of course using smart pointers instead would be possible but is an overkill, since both creation of objects in this new container and the safe deletion of its elements is well encapsulated and safe. smart pointers come to play if you don't know how many copies of your pointers were created but that isn't the case here. other containers, say your own string class, also would use a pointer to std::string internally rather than to derive from std::string, since the standard containers don't have virtual functions and it makes less sense to derive from them.
Query/Maintain a container having two independent keys
- Bjarne Stroustrupsorry, i didn't find Bjarne Stroustrup at the link? Actually, Stroustrup was a C programmer (and loved raw pointers because of this). i read many books of him but can't remember one where he recommended the usage of smart pointers. he also supported to learn C before C++ (and practiced it in his books) what probably makes him not a good advocate for pure OOP with c++.
Note: forget that std::auto_ptr ever existed. Really. You don’t want to use it, ever, especially in containers. It is broken in too many ways.do you agree that those kind of statements are NOT helpful tp propagate the usage of smart pointers?
I have no idea what you mean by thatoh. you never seen an interface that's wants a raw pointer and where you have troubles to feed it with a smart pointer? what advice do you give when a void pointer has to be passed for use in a callback?
No, I believe auto_ptr is evil and should never have been part of the standard.
Firstly, a number of experts on this site critique code saying that they highly recommend that STL containers should not hold pointers. If anyone can clarify the pitfalls and work-arounds of doing this, please let me know.
Here is an outline of how the code currently exists. My change will have to be in Insert_New_Ts.
Open in new window