Карта сайта Kansoftware
НОВОСТИУСЛУГИРЕШЕНИЯКОНТАКТЫ
Разработка программного обеспечения

Simple Bimap

Boost , Chapter 1. Boost.Bimap , Examples

Boost C++ Libraries

...one of the most highly regarded and expertly designed C++ library projects in the world. Herb Sutter and Andrei Alexandrescu, C++ Coding Standards

PrevUpHomeNext

Вот пример из одноминутного учебного раздела.

Перейти к исходному коду

#include <string>
#include <iostream>
#include <boost/bimap.hpp>
template< class MapType >
void print_map(const MapType & map,
               const std::string & separator,
               std::ostream & os )
{
    typedef typename MapType::const_iterator const_iterator;
    for( const_iterator i = map.begin(), iend = map.end(); i != iend; ++i )
    {
        os << i->first << separator << i->second << std::endl;
    }
}
int main()
{
    // Soccer World cup
    typedef boost::bimap< std::string, int > results_bimap;
    typedef results_bimap::value_type 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 << "The number of countries is " << results.size()
              << std::endl;
    std::cout << "The winner is " << results.right.at(1)
              << std::endl
              << std::endl;
    std::cout << "Countries names ordered by their final position:"
              << std::endl;
    // results.right works like a std::map< int, std::string >
    print_map( results.right, ") ", std::cout );
    std::cout << std::endl
              << "Countries names ordered alphabetically along with"
                    "their final position:"
              << std::endl;
    // results.left works like a std::map< std::string, int >
    print_map( results.left, " ends in position ", std::cout );
    return 0;
}

Вы можете переписать его с помощью тегов, чтобы получить читаемость.

Перейти к исходному коду

#include <iostream>
#include <boost/bimap.hpp>
struct country  {};
struct place    {};
int main()
{
    using namespace boost::bimaps;
    // Soccer World cup.
    typedef bimap
    <
        tagged< std::string, country >,
        tagged< int        , place   >
    > results_bimap;
    typedef results_bimap::value_type 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;
    1for( results_bimap::map_by<place>::const_iterator
            i    = results.by<place>().begin(),
            iend = results.by<place>().end() ;
            i != iend; ++i )
    {
        2std::cout << i->get<place  >() << ") "
                  << i->get<country>() << std::endl;
    }
    std::cout << std::endl
              << "Countries names ordered alfabetically along with"
                 "their final position:"
              << std::endl;
    3for( results_bimap::map_by<country>::const_iterator
            i    = results.by<country>().begin(),
            iend = results.by<country>().end() ;
            i != iend; ++i )
    {
        std::cout << i->get<country>() << " ends "
                  << i->get<place  >() << "º"
                  << std::endl;
    }
    return 0;
}

1

<results.by<place>()>эквивалентно<results.right>.

2

<get<Tag>>работает для каждого вида карты

3

<results.by<country>()>эквивалентно<results.left>.


PrevUpHomeNext

Статья Simple Bimap раздела Chapter 1. Boost.Bimap Examples может быть полезна для разработчиков на c++ и boost.




Материалы статей собраны из открытых источников, владелец сайта не претендует на авторство. Там где авторство установить не удалось, материал подаётся без имени автора. В случае если Вы считаете, что Ваши права нарушены, пожалуйста, свяжитесь с владельцем сайта.



:: Главная :: Examples ::


реклама


©KANSoftWare (разработка программного обеспечения, создание программ, создание интерактивных сайтов), 2007
Top.Mail.Ru

Время компиляции файла: 2024-08-30 11:47:00
2025-05-19 23:42:39/0.0065648555755615/0