Pog*_*ogo 5 c++ boost dictionary boost-bimap
参考我之前提出的有关 boost::bimaps 和 boost 关联属性映射接口的问题,我想为我的 bimap 使用 Put 和 Get 辅助函数。
\n\n参考此处给出的示例代码,我尝试添加以下内容,但由于断言失败而出现很长的编译错误...这是代码:
\n\n#include <boost/bimap.hpp> \n#include <boost/property_map/property_map.hpp> \n#include <boost/bimap/property_map/set_support.hpp>\n#include <iostream> \n\nusing namespace boost; \n\nint main() \n{\n typedef int vertex_descriptor_t;\n typedef boost::bimaps::bimap< vertex_descriptor_t, size_t > vd_idx_bimap_t;\n typedef boost::associative_property_map<vd_idx_bimap_t::left_map> asso_vd_idx_bimap_t;\n\n // define bimap\n vd_idx_bimap_t my_bimap;\n asso_vd_idx_bimap_t my_asso_bimap(my_bimap.left);\n\n typedef typename vd_idx_bimap_t::value_type value_type; \n my_bimap.insert( value_type( 1, 100 ) );\n // print bimap\n for(auto t = my_bimap.left.begin(); t != my_bimap.left.end(); ++t)\n std::cout << t->first << " " << t->second << "\\n";\n\n int z = 1;\n std::cout << "value = " << get ( my_bimap.left, z ) << std::endl; // prints correctly value = 100\n\n\n // ERROR here . \n boost::put( my_asso_bimap, 2, 19 );\n\n } \nRun Code Online (Sandbox Code Playgroud)\n\n它给出的错误为:(一个很长的列表。但我刚刚放了一个片段)
\n\n cannot convert \xc3\xa2boost::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>()\xc3\xa2 (type \xc3\xa2mpl_::failed************ (boost::bimaps::detai\nRun Code Online (Sandbox Code Playgroud)\n\n在 boost 中,还有一个错误导致文件 (property_map.hpp) 的第 364 行出现错误
\n\n put(const put_get_helper<Reference, PropertyMap>& pa, K k, const V& v)\n {\n static_cast<const PropertyMap&>(pa)[k] = v;\n }\nRun Code Online (Sandbox Code Playgroud)\n\n我理解关联地图无法改变数据的错误,因为它引用了左侧地图视图。但是如何使用 put 和 get 辅助函数?
\n\n我可以使用 GET (my_bimap.left, z ) 函数,但无法使用 PUT 函数。我想使用关联属性映射来获取和放置函数来在实际的 bimap 上进行操作,这样我就不必使用 insert( value_type() )...
\n\n我希望我对我的问题足够清楚。请建议。
\n一般来说,您不能通过迭代器更新 bimap 条目:
在大多数情况下,迭代器不能直接修改 Bimap 中存储的关系,因为两侧都用作基于键的集合的键。当 bimap 左视图迭代器被取消引用时,返回类型与 std::pair< const A, const B > 签名兼容。
这就是你的答案。同样,你也不能
my_bimap.left[2] = 19;
Run Code Online (Sandbox Code Playgroud)
现在,阅读更多内容使我“怀疑”以下解决方案:
typedef bm::bimap< vertex_descriptor_t, bm::list_of<size_t> > vd_idx_bimap_t;
Run Code Online (Sandbox Code Playgroud)
免责声明:我不知道这会改变(?)的语义,但它至少看起来支持可写引用。下面的打印样本
1 100
value = 100
1 100
2 42
Run Code Online (Sandbox Code Playgroud)
#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";
}
Run Code Online (Sandbox Code Playgroud)