I have a vector of doubles and I need to get the maximum value within it and then store the value inside an integer variable so that I can compare the value. I have this so far:
vector<double>::iterator result;
result = max_element(zerocrossdata.begin(), zerocrossdata.end());
Anyone know how I can place the value inside a variable? Confused!!
The std::max_element()
function returns an iterator pointing to the maximum element in the vector.
So to get the actual maximum value you just need to dereference the returned iterator like so:
double max_value = *result;
You need to deference the iterator using the * operator, as has already been stated by others:
int max_value = *result;
However, be aware that the result of this operation will be a double , not an int. On almost all platforms, double will be an 8 byte floating point value. Your result could be outside the range of values an int (typically a signed 4-byte integer) can support. This could result in unexpected behaviour, and you may (or may not) get a compiler warning about it, depending on your compiler and settings.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.