4

boost::bimaps および boost associative property maps interface hereに関する以前の質問を参照して、bimap にPut および Get ヘルパー関数を使用したいと考えています。

hereのサンプルコードを参照して、次を追加しようとしましたが、アサーションが失敗したという長いコンパイルエラーが発生しました...コードは次のとおりです。

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

using namespace boost; 

int main() 
{
  typedef int vertex_descriptor_t;
  typedef boost::bimaps::bimap< vertex_descriptor_t, size_t > vd_idx_bimap_t;
  typedef boost::associative_property_map<vd_idx_bimap_t::left_map>   asso_vd_idx_bimap_t;

  // define bimap
  vd_idx_bimap_t        my_bimap;
  asso_vd_idx_bimap_t   my_asso_bimap(my_bimap.left);

  typedef typename vd_idx_bimap_t::value_type value_type;    
  my_bimap.insert( value_type( 1, 100 ) );
  // print bimap
  for(auto t = my_bimap.left.begin(); t != my_bimap.left.end(); ++t)
      std::cout << t->first << " " << t->second <<  "\n";

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


  // ERROR here . 
  boost::put( my_asso_bimap, 2, 19 );

 } 

次のようなエラーが発生します:(長いリストですが、スニペットを入れただけです)

  cannot convert âboost::bimaps::detail::non_mutable_data_unique_map_view_access<Derived, Tag, BimapType>::operator[](const CompatibleKey&)::BIMAP_STATIC_ERROR__OPERATOR_BRACKET_IS_NOT_SUPPORTED360::assert_arg<long unsigned int>()â (type âmpl_::failed************ (boost::bimaps::detai

また、boost のファイル (property_map.hpp) の 364 行目にエラーが発生するエラーが 1 つあります。

  put(const put_get_helper<Reference, PropertyMap>& pa, K k, const V& v)
 {
   static_cast<const PropertyMap&>(pa)[k] = v;
  }

左側のマップ ビューを参照しているため、連想マップがデータを変更できないというエラーを理解しています。しかし、 put および get ヘルパー関数を使用するにはどうすればよいですか?

GET (my_bimap.left, z) 関数を使用できますが、PUT 関数を使用できません。get 関数と put 関数に連想プロパティ マップを使用して実際の bimap を操作したかったので、insert( value_type() ) を使用する必要はありません...

私の問題が十分に明確であることを願っています。提案してください。

4

1 に答える 1

1

一般に、反復子を介して bimap エントリを更新することはできません。

Bimap に格納された関係は、ほとんどの場合、反復子によって直接変更できません。これは、両側がキーベースのセットのキーとして使用されるためです。bimap 左ビュー イテレータが逆参照される場合、戻り値の型は std::pair< const A, const B > とシグネチャ互換です。

だからあなたの答えがあります。同様に、あなたはできませんでした

my_bimap.left[2] = 19;

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

さて、そこをもう少し読むと、次の解決策を「疑う」ことができます。

typedef bm::bimap< vertex_descriptor_t, bm::list_of<size_t> > vd_idx_bimap_t;

免責事項:これが変更されるセマンティクスについてはわかりませんが(?)、少なくとも書き込み可能な参照をサポートしているようです。以下のサンプル印刷

1 100
value = 100
1 100
2 42

Coliruでライブを見る


完全なリスト

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

    // define bimap
    vd_idx_bimap_t        my_bimap;
    asso_vd_idx_bimap_t   my_asso_bimap(my_bimap.left);

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

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

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

    boost::put( my_asso_bimap, 2, 42 );

    for(auto t = my_bimap.left.begin(); t != my_bimap.left.end(); ++t)
        std::cout << t->first << " " << t->second <<  "\n";
 } 
于 2014-02-09T23:59:03.903 に答える