很抱歉提出另一个问题here,我应该更清楚地了解使用boost :: bimaps的意图。
我的密钥,值对在这里都是唯一的。所以我想用键,值来填充bimap。并使用值检索密钥。
参考@sehe提到的我的代码及其解决方案here。以下是我想用代码做的事情:
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];
^
有没有办法,我可以使用,使用左关联属性映射和Get for right associative属性映射? Put和Get函数用于泛型。我可以使用插入等,但我想避免它。 @sehe,谢谢你以前的答案。我应该早点告诉这个问题