Boost C++ Libraries Home Libraries People FAQ More

PrevUpHomeNext

Examples

Simple Bimap
Mighty Bimap
MultiIndex to Bimap Path - Bidirectional Map
MultiIndex to Bimap Path - Hashed indices

This is the example from the one minute tutorial section.

#include <iostream>

#include <boost/bimap/bimap.hpp>

int main()
{
    using namespace bimap;

    // Soccer World cup. (The results are not real)

    typedef bimap<std::string,int> results_bimap;
    typedef results_bimap::relation position;

    results_bimap results;
    results.insert( position("Argentina"    ,1) );
    results.insert( position("Spain"        ,2) );
    results.insert( position("Germany"      ,3) );
    results.insert( position("France"       ,4) );

    std::cout << "Countries names ordered by their final position:"
              << std::endl;

    for( results_bimap::right_iterator i    = results.right.begin(),
                                       iend = results.right.end() ;
         i != iend; ++i )
    {
        std::cout << i->first << ") " << i->second << std::endl;
    }

    std::cout << std::endl
              << "Countries names ordered alfabetically along with"
                 "their final position:"
              << std::endl;

    for( results_bimap::left_iterator  i    = results.left.begin(),
                                       iend = results.left.end() ;
         i != iend; ++i )
    {
        std::cout << i->first << " ends " << i->second << "º" << std::endl;
    }

}

You can rewrite it using tags to gain readability.

#include <iostream>

#include <boost/bimap/bimap.hpp>

struct country  {};
struct place    {};

int main()
{
    using namespace bimap;

    // Soccer World cup. (The results are not real)

    typedef bimap
    <
        tagged< std::string, country >,
        tagged< int        , place   >

    > results_bimap;

    typedef results_bimap::relation position;

    results_bimap results;
    results.insert( position("Argentina"    ,1) );
    results.insert( position("Spain"        ,2) );
    results.insert( position("Germany"      ,3) );
    results.insert( position("France"       ,4) );

    std::cout << "Countries names ordered by their final position:"
              << std::endl;

    typedef iterator_type_by<place,results_bimap>::type iterator_by_place;
    for( iterator_by_place i    = map_by<place>(results).begin(),
                           iend = map_by<place>(results).end() ;
         i != iend; ++i )
    {
        std::cout << get<place>(*i) << ") " << get<country>(*i) << std::endl;
    }

    std::cout << std::endl
              << "Countries names ordered alfabetically along with"
                 "their final position:"
              << std::endl;

    typedef iterator_type_by<country,results_bimap>::type iterator_by_country;
    for(  iterator_by_country i    = map_by<country>(results).begin(),
                              iend = map_by<country>(results).end() ;
         i != iend; ++i )
    {
        std::cout << get<country>(*i) << " ends " << get<place>(*i) << "º" << std::endl;
    }

}
Copyright © 2006 Matias Capeletto

PrevUpHomeNext