【发布时间】: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