【问题标题】:Iterator usage - Lint warning迭代器使用 - Lint 警告
【发布时间】:2012-02-05 20:24:42
【问题描述】:

我不熟悉迭代器的使用。我使用了下面的代码,其中我使用迭代器解析列表中的所有元素,以确定该元素是否存在于列表中。

list<int> pendingRsp;
list<int>::iterator it1;

for(int i = 1; i <= 5; i++)
   pendingRsp.push_back(i *10);

for(it1 = pendingRsp.begin(); it1 != pendingRsp.end(); it1++)
{
   if((*it1) == 50)
   {
      found = true;   
      break;
   }
}

代码运行良好,但我收到以下 Lint 警告:

Info 1702: operator 'operator!=' 是一个普通函数 'operator!=(const pair,> &, const pair,> &)' 和成员函数 'list::const_iterator::operator!=(const const_iterator &) const'

上面的警告是什么意思?列表中!=运算符的运算符重载实现与迭代器之间是否存在冲突?

【问题讨论】:

  • 与问题无关,但使用std::find而不是for循环。
  • 是的 std::find 更好。感谢您的建议

标签: c++ iterator lint


【解决方案1】:

这正是它所说的意思。列表迭代器是pairpair 有一个operator!= 函数,但列表迭代器类也有它自己的operator!= 函数。由于两个运算符的作用完全相同(因为第一个元素匹配的任何两对也匹配第二个元素),您可以放心地忽略警告。

【讨论】:

  • 这就是我们使用组合而不是公共继承的原因;-)
猜你喜欢
  • 1970-01-01
  • 2014-08-28
  • 2011-05-16
  • 2014-08-23
  • 2010-11-11
  • 1970-01-01
  • 1970-01-01
  • 2012-09-20
  • 1970-01-01
相关资源
最近更新 更多