我将值存储在std::map中
我在映射中找到两个值,并且想要在第一项到最后一项之间进行迭代-但是未实现
void foobar(const DatedRecordset& recs, const double startstamp, const double endtstamp)
{
DatedRecordsetConstIter start_iter = recs.lower_bound(startstamp), end_iter = recs.lower_bound(endtstamp);
// Can't do this .... (<= not defined)
//for (DatedRecordsetConstIter cit = start_iter; cit <= end_iter; cit++ )
/ So have to resort to a hack like this:
for (DatedRecordsetConstIter cit = start_iter; cit != recs.end(); cit++ ) {
if ((*cit).first <= (*end_iter).first){
//do something;
}
else
break;
}
}
}
在两个已知的迭代器之间是否有更优雅的迭代方法?
最佳答案
使用!=
而不是<=
,它将执行您想要的操作。
void foobar(const DatedRecordset& recs, const double startstamp, const double endtstamp)
{
DatedRecordsetConstIter start_iter = recs.lower_bound(startstamp),
end_iter = recs.upper_bound(endtstamp);
for (DatedRecordsetConstIter cit = start_iter; cit != end_iter; ++cit) {
}
}