2012-08-29 17 views
15

Sorunun ilk kısmı, boost :: bimap kullanmayı denememdir, ancak belgeden iki yönlü bir multimap tanımlamanın nasıl olacağı açık değildir.Boost :: Bimap çift yönlü multimap eşdeğeri

Sorunun ikinci kısmı, bir yönde bir harita ve diğer yönde bir çoklu harita olması gerektiğidir, bu destek :: bimap kullanılarak yapılabilir mi?

Bunu deneyimleyen biri var mı, yoksa beni doğru sayfaya yönlendirebilir mi? Tüm belgelerinde olduğu

cevap

15

... http://www.boost.org/doc/libs/1_51_0/libs/bimap/doc/html/boost_bimap/the_tutorial/discovering_the_bimap_framework.html

typedef boost::bimap<bimaps::multiset_of<int>, bimaps::set_of<int>> bimap_t; 

örneği.

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

namespace bimaps = boost::bimaps; 

int main() 
{ 
    typedef boost::bimap<bimaps::multiset_of<int>, bimaps::set_of<int>> bimap_t; 
    typedef bimap_t::value_type value_type; 
    bimap_t bimap; 
    bimap.insert(value_type(1, 1)); 
    bimap.insert(value_type(1, 2)); 
    auto& left = bimap.left; 
    auto it = left.find(1); 
    std::cout << "LEFT" << std::endl; 
    for (; it != left.end(); ++it) 
    { 
     std::cout << it->first << " " << it->second << std::endl; 
    } 
    auto& right = bimap.right; 
    auto r_it = right.find(2); 
    std::cout << "RIGHT" << std::endl; 
    for (; r_it != right.end(); ++r_it) 
    { 
     std::cout << r_it->first << " " << r_it->second << std::endl; 
    } 
} 
9

foreverr gelen cevap Sorunuzun ilk bölümü için kısmen doğru (iki yönlü Multimap nasıl tanımlanacağını?).

İkinci kısım için (bir yönde bir harita olan bir bimap ve diğer yönde bir multimap erişilmesi) Bu doğru değildir.

erişmek için bir doğru yolu [http://rextester.com/BXBDHN12336] olacaktır:

//bimap operations 

#include <boost/bimap.hpp> 
#include <boost/bimap/set_of.hpp> 
#include <boost/bimap/multiset_of.hpp> 

int main() 
{ 
    typedef boost::bimap<boost::bimaps::multiset_of<int>, boost::bimaps::set_of<int>> bimap_t; 
    typedef bimap_t::value_type value_type; 
    bimap_t bimap; 
    bimap.insert(value_type(1, 1)); 
    bimap.insert(value_type(10, 50)); 
    bimap.insert(value_type(1, 2)); 
    bimap.insert(value_type(9, 15)); 

    typedef bimap_t::left_const_iterator l_itr_t; 
    typedef std::pair<l_itr_t,l_itr_t> l_itr_range_t; 

    l_itr_range_t ii = bimap.left.equal_range(1); 

    std::cout << "LEFT" << std::endl;   
    for(l_itr_t it = ii.first; it != ii.second; ++it) 
    { 
    std::cout << "Key = " << it->first << " Value = " << it->second << std::endl; 
    } 

    std::cout << "RIGHT" << std::endl; 
    std::cout << "Key = " << 1 << " Value = " << bimap.right.at(1) << std::endl; 
} 

stdout: 
LEFT 
Key = 1 Value = 1 
Key = 1 Value = 2 
RIGHT 
Key = 1 Value = 1 

foreverr dan örnek başka çift eklediğinizde çünkü eklenen verilerin düzenin çalışmak ancak sonuç kontrol etmek 'görünüyor' son bimap.insert (value_type (9, 15)) ;:

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

namespace bimaps = boost::bimaps; 

int main() 
{ 
    typedef boost::bimap<bimaps::multiset_of<int>, bimaps::set_of<int>> bimap_t; 
    typedef bimap_t::value_type value_type; 
    bimap_t bimap; 
    bimap.insert(value_type(1, 1)); 
    bimap.insert(value_type(1, 2)); 
    bimap.insert(value_type(9, 15)); 
    auto& left = bimap.left; 
    auto it = left.find(1); 
    std::cout << "LEFT" << std::endl; 
    for (; it != left.end(); ++it) 
    { 
     std::cout << it->first << " " << it->second << std::endl; 
    } 
    auto& right = bimap.right; 
    auto r_it = right.find(2); 
    std::cout << "RIGHT" << std::endl; 
    for (; r_it != right.end(); ++r_it) 
    { 
     std::cout << r_it->first << " " << r_it->second << std::endl; 
    } 
} 

stdout: 
LEFT 
1 1 
1 2 
9 15 
RIGHT 
2 1 
15 9 
+1

"Çalıştığım" ile ne demek istediğinden emin değilim. Aldığınız çıktı, & r_it, find() ile multimap'ın ortasında bir yere başlatılmış olduğundan. Begin() ile başlatılırsa, üç çiftin tümü düzgün yazdırılır. – namezero

+1

Evet, ancak buradaki nokta üç çift basmak değil. Buradaki nokta, bimap'a ulaşmak (soldan ve/veya sağdan) ve verilen bir anahtarın çiftlerini almaktır. Bu yanıtı, denk_dizini kullanarak bunu gerçekleştirebileceğiniz bir örnek dahil olmak üzere düzenledim. Umarım bu yardımcı olur. Bu cevabı güncellemek için –

+0

@JavierBravo kudos. Ancak, aslında tamamen arsa özledim: ** [Live On Coliru] (http://coliru.stacked-crooked.com/a/39ddf19cff3e97d8) ** – sehe

İlgili konular