If I have a vector v={1,2,4,6,8} and I find my maximum element of this vector using this:
"
vector<int>::iterator iter_max = max_element(v.begin(), v.end());
cout<<"Largest element is "<< *iter_max;
"
which works fine. But I need to know the position of this max element. Is it second, third or fourth in my vector structure? What is the position of my max element in my vector? Can anyone help please?

3
Contributors
7
Replies
8
Views
8 Years
Discussion Span
Last Post by JasonHippy

Okay guys, I tried and finally got it. Thanks though for reading.

I could be wrong, but I don't think that theres a way of getting your max_iter iterator to give away the position of max_element...Unless Torture works heh heh...Bring on the COMFY CHAIR! :D (Apologies for the Monty Python...I couldn't resist!)

Anyway, this is about the only thing I could come up with:
(BTW: I've not compiled or tested this snippet, but you'll get the general idea!)

``````// ....your headers and code from before your snippet goes here

vector<int> v = {1,2,4,6,8}

// you've found max_element
vector<int>::iterator iter_max = max_element(v.begin(), v.end());
cout << "Largest element is " << *iter_max << endl;

// iterate through the vector again, but this time we'll count the number of iterations until we reach iter_max
// so we'll use a new iterator and compare it to iter_max
int index=0;
bool isFound=false;
for(vector<int>::iterator iter = v.begin(); iter!=v.end(); ++iter, ++index)
{
if(iter==iter_max)
{
isFound=true
break;
}
}

if(isFound)
cout << "The index of the largest element is: " << index << endl;
else
cout << "Doh!...Something went wrong!" << endl;

OK, it's probably not ideal, but that's the quickest and simplest thing I could think of...Probably OK if you're only using a small vector, but not so good if you've got a large one to iterate through!

Cheers for now,
Jas.

Damn, I was too late! (probably wrong too! heh heh!)

HI Jason,

I liked your stuff. Cool. But I think I found a simpler way :

``````vector<int>::iterator iter_max = max_element(v.begin(), v.end());
int x= *iter_max    //   taking value in x

//Now define another iterator say "it"
vector<int>::iterator  = find(v.begin(), v.end(), x);
if (it != v.end())
{
cout<<"Position is "<<distance(v.begin(), it);
}
// distance(v.begin(), it) : this command gives me direct answer. That's so simple.
But thnx for response. Good day !!
``````

Edited by Nick Evan: Fixed formatting

HI Jason,

I liked your stuff. Cool. But I think I found a simpler way :

You're very close, but an even better way to do it is like this:

``````vector<int> vect;

/* Do stuff with vect */
vector<int>::iterator iter_max = max_element(vect.begin(), vect.end());
cout << "Max is: " << *iter_max << " Position is: " <<distance(vect.begin(), iter_max);``````

No need for extra iterators which improves speed and readability!

And next time when posting code: use code-tags

Edited by Nick Evan: n/a

Very handy..Not seen distance used before!

Oh ya niek_e, that's right. It's better and no need for an extra iterator. Thnx for that.

Nice one niek_e!
I was sure there was a better way of doing things...But for the life of me I couldn't think of it!
I've not seen distance before (must've missed it in my std library reference book)...That's another one for me to add to my std lib arsenal...I'll have to remember that one!

Cheers for now,
Jas.

Edited by JasonHippy: n/a