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)
{

for (int i = 1; i < size; i++)
{
winInd = 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.

You have to think of iterators like pointers. The idea of iterators is to mimic this C-like for-loop:

``````int my_array[10];  //static array;
for(int* ptr = my_array;  //get a pointer to the start of the array.
ptr != my_array + 10; //check that the pointer is not at the end of the array.
++ptr)                //increment the pointer.
*ptr = 0;               //dereference the pointer to access the value.``````

The use of iterators is very similar, except that the start and end iterator are obtained with begin() and end(). So, the arithmetic (e.g. incrementing the iterator) is the same as for pointers (although iterators may not support all operations), and you need to dereference them to obtain the value at that position. So, your function could be this:

``````int winnerIndex(const vector<int>& list) //notice the use of const-reference, and size is not necessary.
{
int winInd = 0;
vector<int>::const_iterator intWinner; //if you don't modify the values, use a const_iterator.

for (intWinner = list.begin(); intWinner != list.end();
++intWinner)

if (*intWinner > list[winInd])
{
winInd = intWinner - list.begin(); //iterator difference will give you the index.
}

return winInd;
}``````

But, actually, the C++ standard libraries have a function to find the max_element of a list, which is called like this:

``````int winnerIndex(const vector<int>& list) {
return std::max_element(list.begin(), list.end()) - list.begin();
};``````

Thanks Mikael,
That makes so much more sense now. I didn't even think of removing size, since with the iterator I wouldn't need it. The point of using a const help also. I have another function that I probably should be using that on as well.
Thanks again.

Yeah, you should not pass large objects by value, so a const-reference is preferred to avoid the expensive copying.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.