加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c – 使用BOOST :: associative属性映射插入boost :: BIMAP …失

发布时间:2020-12-16 07:07:45 所属栏目:百科 来源:网络整理
导读:参考我之前提到的关于boost :: bimaps和boost关联属性映射接口 here的问题,我想为我的bimap使用Put和Get辅助函数. 参考here给出的示例代码,我尝试添加以下内容,我得到一个长编译错误断言失败…这是代码: #include boost/bimap.hpp #include boost/property_
参考我之前提到的关于boost :: bimaps和boost关联属性映射接口 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 aboost::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>()a (type ampl_::failed************ (boost::bimaps::detai

还有一个错误导致我在boost中的文件(property_map.hpp)的第364行出错

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函数.我想使用关联属性映射来获取和放置函数来操作实际的bimap,这样我就不必使用insert(value_type())…

我希望我对自己的问题足够清楚.请建议.

解决方法

通常,您无法通过迭代器更新bimap条目:

The relations stored in the Bimap will not be in most cases modifiable directly by iterators because both sides are used as keys of key-based sets. When a bimap left view iterator is dereferenced the return type is signature-compatible with a 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

见它Live On 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,42 );

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读