STL algorithm function missing position info?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • thomas

    STL algorithm function missing position info?

    ---code---
    int func(int &elem) const{
    return elem+2;
    }
    vector<intvec(3 0,0);
    foreach(vec.beg in(), vec.end(), func);
    ----code-----

    As we see that the "func" function can only do operations on the
    "elem" itself, not the iterator.
    But if I want to do this:

    for each element in vector vec
    *elem = iter - vec.begin(); //its position in the vector.

    how can I do this? in the "func" function, I only can see the
    "elem"(int) , I cannot get the "iter"(iterator ), so the position info
    is missing?

    Of course I know that I can do it by a for loop, but any other way to
    do it?
Working...