【问题标题】:std::multimap error when calling end()调用 end() 时出现 std::multimap 错误
【发布时间】:2012-05-23 18:35:22
【问题描述】:

如何检查多图的元素是否存在?
使用此代码:

typedef std::multimap<std::string, std::string> TagVal;
TagVal tv;
//... add values to tv ...
TagVal::const_iterator it = tv.find("abc");
if(it == TagVal::end())    // <--- ERROR
    cerr << "Error";

我得到以下编译时错误:

错误:无法调用成员函数'std::multimap<...>::iterator std::multimap<...>::end() ... 没有对象。

平台:Linux、GCC 4.5.1

【问题讨论】:

    标签: c++ null iterator multimap


    【解决方案1】:

    原因是end 不是静态方法,它必须在您从中获取迭代器的对象上调用:

    if(it == tv.end())
        cerr << "Error";
    

    【讨论】:

      【解决方案2】:

      因为您已将tv 初始化为

      TagVal tv;

      你必须调用 multimap 类的 end() 函数:

      it == tv.end()

      因为 end() 是在该对象上调用的,它不是静态方法。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2020-08-18
        • 1970-01-01
        • 2010-09-15
        • 2017-07-20
        • 1970-01-01
        • 2011-02-08
        • 1970-01-01
        相关资源
        最近更新 更多