Boost C++ Libraries Home Libraries People FAQ More

PrevUpHomeNext

Unconstrained Sets

Unconstrained sets allow the user to disable one of the views of a bimap. Doing so makes the bimap operations execute faster and reduces memory consumption. This completes the bidirectional mapping framework by including unidirectional mappings as a particular case.

Unconstrained sets are useful for the following reasons:

Given this bimap instance,

typedef bimap< std::string, unconstrained_set_of<int> > bm_type;
typedef bm_type::left_map map_type;

bm_type bm;
map_type & m = bm.left;

or this standard map one

typedef std::map< std::string, int > map_type;

map_type m;

The following code snippet is valid

m["one"] = 1;

assert( m.find("one") != m.end() );

for( map_type::iterator i = m.begin(), iend = m.end(); i != iend; ++i )
{
    1++(i->second);
}

m.erase("one");

1

The right collection of the bimap is mutable so its elements can be modified using iterators.

But using a bimap has some benefits

typedef map_type::const_iterator const_iterator;
typedef std::pair<const_iterator,const_iterator> const_range;

1const_range r = m.range( "one" <= _key, _key <= "two" );
for( const_iterator i = r.first; i != r.second; ++i )
{
    std::cout << i->first << "-->" << i->second << std::endl;
}

m.modify_key( m.begin(), _key = "1" );

1

This range is a model of BidirectionalRange, read the docs of Boost.Range for more information.

Go to source code


PrevUpHomeNext