Categories: MSDN / DotNet / Java / Scripts / Linux / PHP Ask - La ask - La Answer

Why min_element gives an error?

This fragment of the code :
for (int z=0; z<=size; z++)
{
vector<double>::iterator iter = min_element(diff.begin(), diff.end());

//itr = min_element( diff.begin(), diff.end() );
cout<<*iter;
int t3 = iter+1;
int t4 = iter-1;
cout<<"The upper Energy value :"<<energy.at(t3)<<endl;
cout<<"the lower Energy value : "<<energy.at(t4)<<endl;

}//end of the for statement for the min

Gives the following error:
cannot convert
`__gnu_cxx::__normal_iterator<double*, std::vector<double, std::allocator<double> > >' to `int' in initialization

Does anyone have any idea about this error?
Raj
[824 byte] By [maheshrajn] at [2007-11-11 8:08:54]
# 1 Re: Why min_element gives an error?
think about these two lines
int t3 = iter+1;
int t4 = iter-1;

are you trying to use iter as an array index?
or are you trying to add 1 to the value of the element that iter is pointing to?
if so, why is one int and the other double?
rssmps at 2007-11-11 21:01:38 >
# 2 Re: Why min_element gives an error?
think of iterators as pointers. As such they can't assigned to ints for instance. You need to dereferrence the iterator first before an assignment:
int t3 = *(iter+1);
Danny at 2007-11-11 21:02:38 >