欢迎来到虫虫下载站 | 资源下载 资源专辑 关于我们
虫虫下载站

tagged_bidirectional_map.cpp

Boost provides free peer-reviewed portable C++ source libraries. We emphasize libraries that work
CPP
字号:
// Boost.Bimap//// Copyright (c) 2006-2007 Matias Capeletto//// Distributed under the Boost Software License, Version 1.0.// (See accompanying file LICENSE_1_0.txt or copy at// http://www.boost.org/LICENSE_1_0.txt)// Boost.Bimap Example//-----------------------------------------------------------------------------// This example shows how to construct a bidirectional map with// multi_index_container.// By a bidirectional map we mean a container of elements of// std::pair<const FromType,const ToType> such that no two elements exists with// the same first or second value (std::map only guarantees uniqueness of the// first member).// Fast lookup is provided for both keys. The program features a tiny// Spanish-English dictionary with online query of words in both languages.//[ code_mi_to_b_path_tagged_bidirectional_map#include <iostream>#include <boost/bimap/bimap.hpp>using namespace boost::bimaps;// tagsstruct spanish {};struct english {};// A dictionary is a bidirectional map from strings to stringstypedef bimap<    tagged< std::string,spanish >, tagged< std::string,english >> dictionary;typedef dictionary::value_type translation;int main(){    dictionary d;    // Fill up our microdictionary.     // first members Spanish, second members English.    d.insert( translation("hola" ,"hello"  ));    d.insert( translation("adios","goodbye"));    d.insert( translation("rosa" ,"rose"   ));    d.insert( translation("mesa" ,"table"  ));    std::cout << "enter a word" << std::endl;    std::string word;    std::getline(std::cin,word);    // search the queried word on the from index (Spanish) */    dictionary::map_by<spanish>::const_iterator it =        d.by<spanish>().find(word);    if( it != d.by<spanish>().end() )    {        std::cout << word << " is said "                   << it->get<english>() << " in English" << std::endl;    }    else    {        // word not found in Spanish, try our luck in English        dictionary::map_by<english>::const_iterator it2 =            d.by<english>().find(word);        if( it2 != d.by<english>().end() )        {            std::cout << word << " is said "                      << it2->get<spanish>() << " in Spanish" << std::endl;        }        else        {            std::cout << "No such word in the dictionary" << std::endl;        }    }    return 0;}//]

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -