Re: Efficient insertion in a std::multimap
On 22 Aug, 20:43, Sam <s...@email-scan.com> wrote:
Barry writes:
Hi,
Hope this doesn't get lost beneath all the spam.
I have the following container which I wish to create and store
objects of type MyObject in-
class Phase : public std::multimap<double, MyObject>
{
public:
Phase();
};
Phase::Phase()
{
MyObject myObject;
std::pair<double,Note> pair(0.0,myObject);
insert (pair);
}
A lot of copy constructors are being called for MyObject which I'd
like to avoid. But first, I'm not understanding what is happening for
the line: insert (pair);. Here, the copy constructor is called twice
and the default destructor once which suggests to me that a temp
object is create, but why?
"std::pair<double,Note> pair(0.0,myObject)" is the first copy constructor=
..
The second copy constructor occurs in the insert() method.
Finally, is the=
re a way to cut down on all
the copies?
Store pointers, instead of the actual objects. Of course, that creates a
whole bunch of other issues.
application_pgp-signature_part
< 1KVisaH=E4mta
Thanks for the reply. How might I use pointers to do this since I wish
to store many MyObject objects within a Phase Object? If I were to use
pointers, then I would do something like this -
Phase::Phase()
{
MyObject* myObject = new MyObject();
std::pair<double,Note*> pair(0.0,myObject);
insert (pair);
}
But I will be storing a whole bunch of MyObject objects within a Phase
object, so I'm still going to need to keep track of them in order to
delete them in Phase's destructor. But how would I keep track of them?
Thanks again,
Barry