【问题标题】:error using find() in boost associative property map in BOOST graph在 BOOST 图中的 boost 关联属性映射中使用 find() 时出错
【发布时间】:2014-02-27 01:34:23
【问题描述】:

我想实现一个 ifexists() 模板函数来检查一个键是否存在于地图中。

如果它是 std::map ,我可以在模板中使用 find() 函数,因此可以实现我的模板 ifexists() 函数。 (下)

但我使用 Boost::associative_property_map,它引用了我的 std::map。我可以在这个关联属性映射上使用 get 和 put 函数。

std::map< int, int > mymap;
boost::associative_property_map< std::map< int, int > > asso_map ( mymap );

//asso_map stores reference to mymap. and i can use get and put functions on this associative map.

现在,如果我将这个 asso_map 传递给我的模板函数“ ifexists(asso_map, key)”,它会抛出以下错误:

error: âclass boost::associative_property_map<std::map<int, int > >â has no member named âendâ
error: âclass boost::associative_property_map<std::map<int, int > >â has no member named âfindâ

这是我的模板功能代码:

template <typename map_t, typename key_t>
bool exists_in(map_t map, key_t key)  
{  
  return map.find(key) != map.end();
}

//function call:--> v is int.
if( exists_in ( asso_map, v) ) ... Error (above) 
if( exists_in ( mymap, v) ) ... Correct 

“find”不是成员函数的错误是正确的,在查看文档here后,很明显它没有find()这样的成员函数。

但是,如果我将实际地图(即 mymap )传递给模板函数,它就可以工作。但我不想这样做,因为这样就没有使用 boost 的关联属性映射的意义。

我想找到一种方法,可以将 find() 用于我的 associative_property_map asso_map。我知道我可以遍历 assoc_map 并找到出路。但想知道我是否可以以某种方式使用 find()

我希望它更清楚。很抱歉有歧义。

【问题讨论】:

  • 问题是什么?从您的消息中不清楚。更重要的是,您的代码混合了 C++ 代码和未真正格式化为 C++ cmets 的 cmets。您能否编辑您的问题以解决这两个问题?
  • 嗨,我已经更新了这个问题。我希望现在更清楚了。

标签: c++ boost c++11 map boost-graph


【解决方案1】:

不幸的是,boost::associative_property_map 不提供对其使用的关联容器的访问权限。您不能访问std::mapfind() 函数,也不能迭代它的内容。您只能访问Lvalue Property Map的API。

【讨论】:

  • 我知道,这会妨碍附加功能​​。我在 boost-users 列表上问了同样的问题,并等待他们的答复。如果他们可以添加这个接口,那将非常有用。他们没有提供这样的接口应该是有原因的。 Get、Put 和 Find 对于使用属性映射非常重要。
  • @user2404319 如果 Boost 邮件列表为您提供了更有用的答案,请在此处发布另一个答案,或编辑我的答案。
猜你喜欢
  • 2012-01-04
  • 2014-03-07
  • 1970-01-01
  • 2013-12-18
  • 2021-03-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-12-13
相关资源
最近更新 更多