【发布时间】:2015-05-03 09:50:48
【问题描述】:
所有,
std::map<int, std::string> addressee;
std::map<int, std::string>::iterator it1, it2;
for( it1 = addressee.begin(); it1 != addressee().end(); it1++ )
{
bool found = false;
for( it2 = it1 + 1; it2 != addressee.end() && !found; it2++ )
{
if( it1->second == it1->second )
{
printf( "Multiple occurences of addressees found" );
found = true;
}
}
}
gcc 报错:no match for operator+。
此代码是我现在尝试做的简化版本。我想我可以使用 std::advance(),但它似乎只是在浪费函数调用。
有没有更好的解决方法?
【问题讨论】:
-
“浪费了一个函数调用”。函数调用不是“浪费”。
-
it2 = it1; ++it2;在循环之前然后for(; it2 != ...) -
@MooingDuck,不,这不是浪费。但它需要一些我宁愿避免的额外操作。这需要一些时间来完成。
-
@Rado,最简单的解决方案。谢谢你。我想我必须从不同的角度来看待它。
-
@Igor:你在想什么“额外的操作”?你认为它“需要”多少“时间”?你量过吗?如果你这样做了,你会感到惊讶。现在你只是在猜测,从这些猜测中得出错误的结论,然后使用这些错误的结论得出错误的解决方案(或者更确切地说,跳过正确的解决方案)。
标签: c++ dictionary iterator