it seems that I have the equivalence of Rate->Maturity and not Maturity->Rate. I mean the index and the value are reversed. Could you help me understand why?
My second question is : why I get [0]=0 at the begining? i thought that i started filling the vector from 0!!!
Note that the postfix ++ operator returns the value of the iterator before ++ was applied. You also can't be sure if the first itt on that line will use itt after, or before ++ has been applied (but for you it seems to be after). You might want to use +1 or std::next instead.
cout << *itt << "-->" << *(itt+1) << endl;
cout << *itt << "-->" << *next(itt) << endl;
If you find it easier to use a vector you could do something like this:
In C++11 (and later) you can write the above loop even more compact using a range-based for loop. It's still important to understand iterators when using range-based for loops because that is what is used behind the scene. Invalidating the iterators, which could happen if you add or remove elements from the vector that you iterate over, is an equally big problem in both cases.
cout << *itt << "-->" << *(itt++) << endl;
First of all, that is a post-increment. Post-increment returns old value, what the object had before increment.
More important issue (and this is not specific to iterators) is that compiler can evaluate operands in any order. Your environment apparently does, in pseudo:
1 2 3 4 5
X = itt;
++itt;
Z = *X;
Y = *itt;
cout << Y <<"-->" << Z << endl;
On some other environment the order is different.
cout << *itt << "-->" << *(itt++) << endl;
You could do:
The 0-->0: vector<vector<double>> ResultVector(1,vector<double>(2));
Create ResultVector that has one element and initialze that element with vector<double>(2) object.
vector<double>(2)
Create a vector that contains two double elements. I cannot remember whether they are zero-initialized by standard, or is it just your compiler's decision.
In other words: you add explicitly the "unexpected" entry.
But why use a vector at all? The "two doubles" nature is known at compile-time.
1 2 3 4 5
vector<std::pair<double,double>> ResultVector;
// or
vector<std::array<double,2>> ResultVector;
ResultVector.reserve( RateCurve.size() );
It works with next(itt) . Oh so you mean that next(itt) is not equivalent to itt++ !! why is that? i expect that doing itt++ in for (vector<vector<double>>::iterator itt=ResultVector.begin();itt!=ResultVector.end();itt++) helps to scroll through the vector in an ordered way.
std::next does not modify the iterator. It simply returns the iterator that comes after.
The ++ operator comes in two versions. The postfix (itt++) and the prefix (++itt). Both of them modify the iterator in the same way. The difference between them is that the postfix operator returns the old iterator value while the prefix operator returns the new iterator value.
The for-loop increments the iterator without making use of the returned value, so the use of pre-increment is recommended. (The compiler might be able to optimize needless code (the temp) out, but why assume when you can be explicit?)