Hello,
I'm attempting to write a function that accepts a vector "list" and it's "size" as arguments and using an iterator, step through the list to find the one with the highest value.
For instance, here is the non-iterator function:
int winnerIndex(vector<int> list, int size)
{
int votes = list[0];
for (int i = 1; i < size; i++)
if (votes < list[i])
{
winInd = i;
votes = list[i];
}
return winInd;
}
This worsks fine, but I'm trying to understand iterators and how they can be used, so I've created my function as so:
int winnerIndex(vector<int> list, int size)
{
int winInd = 0;
vector<int>::iterator intWinner;
for (intWinner = list.begin(); intWinner != list.end();
++intWinner)
if (list[intWinner] > list[winInd])
{
winInd = *intWinner;
}
return winInd;
}
But I keep getting a vector subscript out of range error. Can anyone help me understand what I'm doing wrong here? I thought that I would be passing the position location of the vector element to winInd, but it seems like I am still passing the value.