Dereference and Increment on vector Iterators
The operations on iterator types let us retrieve element to which an iterator refers and let us move an iterator from one element to another.
- Iterator types use the dereference operator (the * operator) to access the element to which the iterator refers:
*iter=0; //The effect of this statement is to assign 0 to that element
- The dereference operator returns the element that the iterator currently denotes.Assuming iter refers to the first element of the vector,then *iter is the same element as ivec[0].The effect of this statement is to assign 0 to that element.
A Program that Uses Iterators
- Assume we had a vector named ivec and we wanted to reset each of its elements to zero.We might do so by using a subscript:
for(vector<int>::size_type ix=0;ix!=ivec.size();++ix)
ivec[ix]=0; //reset all the elements in ivec to 0
- A more typical way to write this loop would use iterators:
for(vector<int>::iterator iter=ivec.begin();iter!=ivec.end();++iter
The First | The Second |
---|---|
subscript | ['sab skript]下标 |
norm | 范数 |