La risposta da foreverr è parzialmente corretto per la prima parte della sua domanda (come definire una multimap bidirezionale?).
Per la seconda parte (accesso a una bimap che è una mappa in una direzione e una multimappa nella direzione opposta) Non è corretta.
Un modo corretto di accesso sarebbe [http://rextester.com/BXBDHN12336]:
//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
L'esempio da foreverr 'sembra' al lavoro per l'ordine dei dati inseriti, ma controllare i risultati quando si inserisce un altro paio a alla fine 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
fonte
2013-04-13 14:59:11
Non sono sicuro di cosa intendi con "sembra funzionare". L'output che stai ottenendo è perché esso & r_it sono inizializzati in qualche punto nel mezzo della multimap con find(). Se inizializzato con begin(), tutte e tre le coppie vengono stampate correttamente. – namezero
Sì, ma il punto qui non è stampare le tre coppie. Il punto è accedere al bimap (da sinistra e/o da destra) e recuperare le coppie per un dato tasto. Ho modificato la mia risposta includendo un esempio in cui è possibile soddisfare questo utilizzando equal_range. Spero che aiuti. –
@JavierBravo kudos per l'aggiornamento di questa risposta. Tuttavia, hai completamente perso la trama: ** [Live On Coliru] (http://coliru.stacked-crooked.com/a/39ddf19cff3e97d8) ** – sehe