寻找具有非唯一键或值的双向地图 [英] seeking bidirectional map with non-unique keys or values

查看:53
本文介绍了寻找具有非唯一键或值的双向地图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要一个带有非唯一键或值的双向容器。 multimap不起作用,因为它是单向的,并且boost> :: bimap仅允许唯一的键和值。标准或boost库中是否有这样的容器?否则:任何实现这种结构的指针/文章将不胜感激。最多只能有100个左右的元素对。感谢任何帮助,谢谢珍妮特

I need a bidirectional container with non-unique keys or values. multimap doesn't work, because it's unidirectional and boost>::bimap only allows unique keys and values. Any such container in the standard or boost libs? Otherwise: any pointers/articles to implement such a structure would be appreciated. Only up to 100 or so element-pairs are expected. Any help appreciated, thanks, Jeanette

推荐答案

实际上boost :: bimap确实允许使用非唯一键:

Actually boost::bimap does allow non-unique keys:

http://www.boost.org/doc/libs/release/libs/bimap/doc/html/boost_bimap/the_tutorial/differences_with_standard_maps.html

示例:

#include <boost/bimap.hpp>
#include <boost/bimap/multiset_of.hpp>
#include <string>
#include <iostream>

namespace bm = boost::bimaps;

int main()
{
    using mybimap = bm::bimap< bm::multiset_of<std::string>, int >;
    mybimap map;

    map.left.insert( mybimap::left_value_type( "orange", 1 ) );
    map.left.insert( mybimap::left_value_type( "apple", 42 ) );
    map.left.insert( mybimap::left_value_type( "orange", 7 ) );    

    auto rng = map.left.equal_range( "orange" );
    for( auto it = rng.first; it != rng.second; ++it )
        std::cout << it->first << ": " << it->second << "\n";
}

实时演示:

http://coliru.stacked-crooked.com/a / 2efdc80cde5f2933

这篇关于寻找具有非唯一键或值的双向地图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆