【发布时间】:2016-06-22 17:20:19
【问题描述】:
为什么下面代码中的迭代不起作用?我的意思是迭代器是递增的,但评估这个表达式o!=RateCurve.end() 总是给true。
我需要这样一个函数,因为我在地图的包装中使用它来构建利率曲线。
#include <iostream>
#include <algorithm>
#include <math.h>
#include <string>
#include <map>
#include <exception>
#include <vector>
#include <stdio.h>
using namespace std;
map<double, double>::iterator getIt(map<double, double> o){
return o.begin();
}
int main ()
{
map<double, double> RateCurve;
RateCurve[3.3 ]=0.034 ;
RateCurve[1.2 ]=0.03 ;
RateCurve[0.2 ]=.001 ;
RateCurve[6.1 ]=.023 ;
map<double, double>::iterator o=getIt(RateCurve);
while (o!=RateCurve.end()){
cout << "RateCurve[" << o->first << "] = " << o->second << endl;
++o;
}
}
【问题讨论】:
-
这与您的问题无关,但是当您可以直接调用
begin()时,我不明白为什么您需要定义getIt- 您的函数只不过是后者。 -
for ( auto const& o : RateCurve ) { ... }干净多了。