STL Map在搜索尝试时抛出错误

时间:2011-12-17 08:40:25

标签: c++ string stl maps

我正在尝试创建一个充满字符串的映射作为键和整数作为值。当我尝试用它搜索时,问题就开始了。谁能告诉我哪里出错了?是不是我在同一个声明中有两张地图?

    invale is "ale"
    roomno = 2;
    // roomlist is a map
    // rinventory is another map

    if( roomlist[roomno].rinventory.find( invale ) != map<string, int>::end());

我得到的错误如下。什么超载功能?这确实是一个冗长的错误。

error C2668: 'std::_Tree<_Traits>::end' : ambiguous call to overloaded function
1>        with
1>        [
1>            _Traits=std::_Tmap_traits<std::string,int,std::less<std::string>,std::allocator<std::pair<const std::string,int>>,false>
1>        ]
1>        c:\program files\microsoft visual studio 9.0\vc\include\xtree(569): could be 'std::_Tree<_Traits>::const_iterator std::_Tree<_Traits>::end(void) const'
1>        with
1>        [
1>            _Traits=std::_Tmap_traits<std::string,int,std::less<std::string>,std::allocator<std::pair<const std::string,int>>,false>
1>        ]
1>        c:\program files\microsoft visual studio 9.0\vc\include\xtree(564): or       'std::_Tree<_Traits>::iterator std::_Tree<_Traits>::end(void)'
1>        with
1>        [
1>            _Traits=std::_Tmap_traits<std::string,int,std::less<std::string>,std::allocator<std::pair<const std::string,int>>,false>
1>        ]
1>        while trying to match the argument list '(void)'    

提前谢谢。

3 个答案:

答案 0 :(得分:3)

尝试将其更改为:

if( roomlist[roomno].rinventory.find( invale ) != roomlist[roomno].rinventory.end());

答案 1 :(得分:0)

应该是

if( roomlist[roomno].rinventory.find( invale ) != roomlist[roomno].rinventory.end());

答案 2 :(得分:0)

map::end()方法不是静态的。

正确的方法是这样的:

map<string, int>::iterator it = roomlist[roomno].rinventory.find( invale );
if( it != roomlist[roomno].rinventory.end())
  // do stuff