Position in Vector using STL

后端 未结 3 802
独厮守ぢ
独厮守ぢ 2021-02-04 04:22

im trying to locate the position of the minimum value in a vector, using STL find algorithm (and the min_element algorithm), but instead of returning the postion, its just givin

3条回答
  •  深忆病人
    2021-02-04 05:04

    The short answer to what you think you asked with "How do I determine position in std::vector<> given an iterator from it?" is the function std::distance.

    What you probably meant to do, however, was to get the value for the iterator, which you get by dereferencing it:

    using namespace std;
    vector::const_iterator it = min_element(v2.begin(), v2.end());
    cout << "min value at position " << distance(v2.begin(), it) << " is " << *it;
    

提交回复
热议问题