Boost logo

Boost Users :

Subject: [Boost-users] [Boost.Bimap] templated Bimaps and tagging; efficiency
From: Christoph Duelli (duelli_at_[hidden])
Date: 2008-10-08 12:57:03

I'd like to replace a bidirectional mapping (2 std::maps so far) with
Boost.Bimap. Being based on Boost.MultiIndex I hope there are some space
savings there (also I could not find specifics in the docs).

I have run into some questions/issues that I hope someone can shed light

i) I insert a name/value pair like:
 typedef boost::bimap<std::string, int> MyBimap;
 MyBimap myBimap;
 bool ok = myBimap.insert( typename MyBimap::value_type(name,val) ).second;

Is it possible (w/o checking afterwards) to find out *why* the insert
failed? (It might be a duplicate on the left or the right side. I want to
print a helpful debug message...)

ii) I would like to use a Bimap in a templated class: Works ok.
When I try to tag the bimap, I get the following errors (using gcc 4.1.2)

error: expected primary-expression before 'template'
error: expected `;' before 'template'
(The errors are the calls to by<country>()

// wrapped tagged_simple_bimap.cpp from the Bimap docs in a template class:
template<typename T>
class X
    typedef bimap
        tagged< std::string, country >,
        tagged< T , place >

> results_bimap;

(I have attached the modified example.)

Tagging works if the bimap definition does contain templated types.
Any ideas what I am doing wrong?

iii) I need the order of my insertions at one place in my code.
Do I lose lookup efficiency (on the left side) when I declare my map as:
 typedef bimap <vector_of<std::string>, int>

(My (bi)map is initialized once and then used for lookups only. Is it better
in that case to store the insertion order in a separate vector?

Or should I go for the more awe-inspiring Boost.Multimap if I want a Bimap
(with efficient lookup) *and* have access to the insertion order?

iv) What is the difference between:
 typedef bimap< std::string, int, list_of_relation> ...;
 typedef bimap< vector_of<std::string>, int> ...;

Thank you for a helpful library (collection).

Boost-users list run by williamkempf at, kalb at, bjorn.karlsson at, gregod at, wekempf at