Boost logo

Boost Users :

Subject: Re: [Boost-users] Container with insertion order
From: Igor R (boost.lists_at_[hidden])
Date: 2011-11-30 07:31:47


<...>

> public:
>        V& operator[]( const K& key )
>          {
>            r = new mutable_pair(key,V());
>            c.insert(c.end(),*r);
>            return (*r).second;
>          }
<..>
> I now correctly get two iterators, but in this class don't work the
> assignment: my output is:
> Joe - 0
> Lillo - 0
> Mary - 0

Of course, because insert() copies its parameter, so you actually
return the ref to the original object while the container stores its
copy (thus you also have a memory leak).
The code should be as follows:

V& operator[]( const K& key )
{
  c.insert(c.end(), mutable_pair(key,V()));
  return c.get<1>().find(key)->second;
}


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net