1

ここで別の質問をして申し訳ありませんが、boost::bimaps を使用する意図についてもっと明確にするべきでした。

私のキーと値のペアは、ここでは両方とも一意です。キー、値を使用してbimapを埋めたいです。値を使用してキーを取得します。

@seheが言及した私のコードとその解決策を参照してください。コードでやりたいことは次のとおりです。

1. define a bimap  (my_bimap )
2. define associative property map for left view of bimap  ( right_asso_bimap )
3. define associative property map for right view of bimap  ( left_asso_bimap )
4. fill the bimap using **left** view associative property map. 
   i.e.  use boost::put( left_asso_bimap, key, value )
5. Now using **right** view associative property map, retrieve key for given value
   i.e. use boost::get( right_asso_bimap, value )

これが私のコードです:

#include <boost/bimap.hpp> 
#include <boost/property_map/property_map.hpp> 
#include <boost/bimap/property_map/set_support.hpp>
#include <boost/bimap/list_of.hpp>
#include <iostream> 

using namespace boost; 

int main() {
typedef int vertex_descriptor_t;
namespace bm = boost::bimaps;
typedef bm::bimap< vertex_descriptor_t, bm::list_of<size_t> > vd_idx_bimap_t;
typedef boost::associative_property_map<vd_idx_bimap_t::left_map>    Lasso_vd_idx_bimap_t;
typedef boost::associative_property_map<vd_idx_bimap_t::right_map>   Rasso_vd_idx_bimap_t;

// define bimap and assoc prop map for it
vd_idx_bimap_t        my_bimap;
Lasso_vd_idx_bimap_t  Lmy_asso_bimap(my_bimap.left );
Rasso_vd_idx_bimap_t  Rmy_asso_bimap(my_bimap.right);

typedef typename vd_idx_bimap_t::value_type value_type;    
my_bimap.insert( value_type( 1, 100 ) );
my_bimap.insert( value_type( 2, 200 ) );

int z = 1;
std::cout << "value = " << get ( my_bimap.left, z ) << std::endl;    // prints correctly value = 100

// use left asso bimap for inserting to bimap 
boost::put( Lmy_asso_bimap, 3, 300 );

// print bimap using left view
for(auto t = my_bimap.left.begin(); t != my_bimap.left.end(); ++t)
    std::cout << t->first << " " << t->second <<  "\n";

int y = 2;
z = boost::get( Lmy_asso_bimap, y  );
std::cout << "value = " << z << std::endl;

// Use right asso bimap to get key based on supplied value
y = 100;
**z = boost::get( Rmy_asso_bimap, y );**    // -->> error here 
std::cout << "value = " << z << std::endl;

// print map again. (right view)
for(auto t = my_bimap.right.begin(); t != my_bimap.right.end(); ++t)
    std::cout << t->first << " " << t->second <<  "\n";

} 

エラーは以下のとおりです。

In file included from main.cpp:2:0: /usr/local/include/boost/property_map/property_map.hpp: In instantiation of 'boost::associative_property_map<UniquePairAssociativeContainer>::value_type& boost::associative_property_map<UniquePairAssociativeContainer>::operator[](const key_type&) const [with UniquePairAssociativeContainer = boost::bimaps::views::list_map_view<boost::bimaps::relation::member_at::right, boost::bimaps::detail::bimap_core<int, boost::bimaps::list_of<int>, mpl_::na, mpl_::na, mpl_::na> > boost::associative_property_map<UniquePairAssociativeContainer>::reference = const int&; boost::associative_property_map<UniquePairAssociativeContainer>::value_type = const int; boost::associative_property_map<UniquePairAssociativeContainer>::key_type = int]':

/usr/local/include/boost/property_map/property_map.hpp:357:54:   required from 'Reference boost::get(const boost::put_get_helper<Reference, PropertyMap>&, const K&) [with PropertyMap = boost::associative_property_map<boost::bimaps::views::list_map_view<boost::bimaps::relation::member_at::right, boost::bimaps::detail::bimap_core<int, boost::bimaps::list_of<int>, mpl_::na, mpl_::na, mpl_::na> > > Reference = const int&; K = int]'

main.cpp:42:39:   required from here /usr/local/include/boost/property_map/property_map.hpp:502:20: error: no match for 'operator[]' (operand types are 'boost::associative_property_map<boost::bimaps::views::list_map_view<boost::bimaps::relation::member_at::right, boost::bimaps::detail::bimap_core<int, boost::bimaps::list_of<int>, mpl_::na, mpl_::na, mpl_::na> > >::C {aka boost::bimaps::views::list_map_view<boost::bimaps::relation::member_at::right, boost::bimaps::detail::bimap_core<int, boost::bimaps::list_of<int>, mpl_::na, mpl_::na, mpl_::na> >}' and 'const key_type {aka const int}')
   return (*m_c)[k];
                ^

左連想プロパティ マップを使用して Put し、右連想プロパティ マップを取得する方法はありますか? Put および Get 関数は汎用性のためのものです。インサートなどを使用できますが、避けたいです。@sehe、以前の回答に感謝します。この問題自体をもっと早く話すべきだった

4

0 に答える 0