我在这里如履薄冰,对不起。我以前没有使用过反向迭代器,就像您在我的代码中看到的那样,我还想使用来自另一个类的向量作为迭代器对象:
double indicators::sRSItemp(input* Close1, int StartDay) {
int n = 14;
double rs;
double rsi;
double tmpavl;
double tmpavg;
if (!RSI.empty()) {
for ( vector<double>::reverse_iterator i = Close1->Close.rbegin(); i != Close1->Close.rend(); ++i ) {
if (Close1->Close[i] < Close1->Close[(i + 1)]){
tmpavl = ((AVL[0] * 13 ) + (Close1->Close[(i +1)] - Close1->Close[i] ) / n);
cout << "AVLtmp " << AVL[0] << endl;
cout << "tmpavl " << tmpavl << endl;
AVL.insert(AVL.begin(), tmpavl);
cout << "AVL is " << AVL[0] << endl;
tmpavg = ((AVG[0] * 13 ) / n );
AVG.insert(AVG.begin(), tmpavg);
// cout << "AVG is " << AVG[i] << endl;
}
else if (Close1->Close[i] > Close1->Close[(i + 1)]) {
tmpavg = ((AVG[0] * 13 ) + (Close1->Close[i] - Close1->Close[(i +1)]) / n );
AVG.insert(AVG.begin(), tmpavg);
// cout << "AVG is " << AVG[i] << endl;
tmpavl = ((AVL[0] * 13 ) / n );
AVL.insert(AVL.begin(), tmpavl);
// cout << "AVL is " << AVL[i] << endl;
}
rs = AVG[0] / AVL[0];
rsi = (100.0 - (100.0 / (1.0 + rs)));
RSI.insert(RSI.begin(), rsi);
}
}
return 0;
}
但是当我编译这段代码时,我得到了几个这样的错误:错误:'operator[]'不匹配(操作数类型是'std::vector'和'std::vector::reverse_iterator {aka std::reverse_iterator<__gnu_cxx ::__normal_iterator > >}'),指向我的向量索引??
if (Close1->Close[i] < Close1->Close[(i + 1)]){
就像我说的那样,这对我来说是新领域,我猜错误在于迭代器的声明?当我在其他代码中遍历相同的向量(从前到后)时,没有问题。非常感谢帮助!