[Boost-users] [MultiIndex] insert with and without position hint

0 views
Skip to first unread message

Igor R

unread,
Nov 23, 2009, 2:07:26 PM11/23/09
to boost...@lists.boost.org
Hello,

For some reason, the "hinted" version of insert() returns just an
iterator, while the "unhinted" one returns pair:
std::pair<iterator,bool> insert(const value_type& x);
iterator insert(iterator position,const value_type& x);

I've got a container with 1 ordered unique index. From one hand, I
have to know whether the insertion was succesful, but from the other
hand, 99% of the objects are inserted in their natural ascending
order, so I use the hinted version:
container_.insert(container_.end(), newObj);
So my question is whether there's any performance benefit of the
"hinted" version in such case, or I can move to the simple one w/o any
performance loss?

Thanks.
_______________________________________________
Boost-users mailing list
Boost...@lists.boost.org
http://lists.boost.org/mailman/listinfo.cgi/boost-users

Joaquin M Lopez Munoz

unread,
Nov 24, 2009, 9:09:42 AM11/24/09
to boost...@lists.boost.org
Igor R <boost.lists <at> gmail.com> writes:

> For some reason, the "hinted" version of insert() returns just an
> iterator, while the "unhinted" one returns pair:
> std::pair<iterator,bool> insert(const value_type& x);
> iterator insert(iterator position,const value_type& x);

The only reson for this dissimilarity is that
standard (unique) associative containers are defined
the same way.



> I've got a container with 1 ordered unique index. From one hand, I
> have to know whether the insertion was succesful, but from the other
> hand, 99% of the objects are inserted in their natural ascending
> order, so I use the hinted version:
> container_.insert(container_.end(), newObj);
> So my question is whether there's any performance benefit of the
> "hinted" version in such case, or I can move to the simple one w/o any
> performance loss?

There should be a performance penalty associated
to not using hinted insertion (if you want to
know the impact of this penalty on your particular
app I suggest you do some profiling).

An easy and cheap way to know whether the insertion was
succesful is to check if size() has increased.

Joaquín M López Muñoz
Telefónica, Investigación y Desarrollo

Igor R

unread,
Nov 24, 2009, 9:30:22 AM11/24/09
to boost...@lists.boost.org
> An easy and cheap way to know whether the insertion was succesful is to check if size() has increased.

Thanks, that's what I'm doing now.
I just thought that maybe unhinted version first tries to insert an
element at the end anyway...

Reply all
Reply to author
Forward
0 new messages