当前位置:  编程语言>c/c++

c++类库Boost.Bimap(遍历,插入,查找,删除)参考代码

 
    发布时间:2013-10-24  


    本文导语:  boost的bimap相当于STL的map的升级版本, 具有双向映射. 学过STL的map的童鞋很容易掌握它的使用.不过, 差别肯定是有的. 因为它是双向的, 所以有左右之分. 如: boost::bimap<int,int> bm;bm.left就相当于STL的map, bm.right...

    boostbimap相当于STLmap升级版本, 具有双向映射. 学过STL的map的童鞋很容易掌握它的使用.不过, 差别肯定是有的. 因为它是双向的, 所以有左右之分. 如:

    boost::bimap<int,int> bm;

bm.left就相当于stl的map, bm.right就是把stl中的key-value键值对反过来, 变成value-key, 它相应的first和second也就变成了value和key.

  bimap是boost中很重要的一个容器,可以进行双向的查找和替换,这样弥补了如果map和multimap需要找到data所对应的键值。循环遍历元素,才能找到相应的键值,再删除,最后替换的不足。


参考代码1如下:

#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;
}


输出结果如下:

The output of this program will be the following:
The number of countries is 4
The winner is Argentina
Countries names ordered by their final position:
1) Argentina
2) Spain
3) Germany
4) France
Countries names ordered alphabetically along with their final position:
Argentina ends in position 1
France ends in position 4
Germany ends in position 3
Spain ends in position 2


参考代码二:

// 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)
//  VC++ 8.0 warns on usage of certain Standard Library and API functions that
//  can be cause buffer overruns or other possible security issues if misused.
//  See http://msdn.microsoft.com/msdnmag/issues/05/05/SafeCandC/default.aspx
//  But the wording of the warning is misleading and unsettling, there are no
//  portable alternative functions, and VC++ 8.0's own libraries use the
//  functions in question. So turn off the warnings.
#define _CRT_SECURE_NO_DEPRECATE
#define _SCL_SECURE_NO_DEPRECATE
// Boost.Bimap Example
//-----------------------------------------------------------------------------
#include <boost/config.hpp>
#include <iostream>
#include <boost/tokenizer.hpp>
#include <boost/bimap/bimap.hpp>
#include <boost/bimap/unordered_set_of.hpp>
#include <boost/bimap/list_of.hpp>
using namespace boost::bimaps;
struct counter {
    counter() : c(0) {}
    counter& operator++() { ++c; return *this; }
    unsigned int operator++(int) { return c++; }
    operator const unsigned int() const { return c; }
    private:
    unsigned int c;
};
int main()
{
    //[ code_repetitions_counter
    typedef bimap
    <
        unordered_set_of< std::string >,
        list_of< counter > /*< `counter` is an integer that is initialized
                                in zero in the constructor >*/
    > word_counter;
    typedef boost::tokenizer<boost::char_separator<char> > text_tokenizer;
    std::string text=
        "Relations between data in the STL are represented with maps."
        "A map is a directed relation, by using it you are representing "
        "a mapping. In this directed relation, the first type is related to "
        "the second type but it is not true that the inverse relationship "
        "holds. This is useful in a lot of situations, but there are some "
        "relationships that are bidirectional by nature.";
    // feed the text into the container
    word_counter   wc;
    text_tokenizer tok(text,boost::char_separator<char>(" tn.,;:!?'"-"));
    for( text_tokenizer::const_iterator it = tok.begin(), it_end = tok.end();
         it != it_end ; ++it )
    {
        /*<< Because the right collection type is `list_of`, the right data
             is not used a key and can be modified in the same way as with
             standard maps. >>*/
        ++ wc.left[*it];
    }
    // list words with counters by order of appearance
    /*<< When we insert the elements using the left map view, the element
         is inserted at the end of the list. >>*/
    for( word_counter::right_const_iterator
            wit = wc.right.begin(), wit_end = wc.right.end();
         wit != wit_end; ++wit )
    {
        std::cout << wit->second << ": " << wit->first;
    }
    //]
    return 0;
}


相关文章推荐:


站内导航:


特别声明:169IT网站部分信息来自互联网,如果侵犯您的权利,请及时告知,本站将立即删除!

©2012-2021,,E-mail:www_#163.com(请将#改为@)

浙ICP备11055608号-3