Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using std::max_element on a vector<double>

Tags:

c++

max

vector

min

I'm trying to use std::min_element and std::max_element to return the minimum and maximum elements in a vector of doubles. My compiler doesn't like how I'm currently trying to use them, and I don't understand the error message. I could of course write my own procedure to find the minimum and maximum, but I'd like to understand how to use the functions.

#include <vector> #include <algorithm>  using namespace std;  int main(int argc, char** argv) {      double cLower, cUpper;     vector<double> C;      // Code to insert values in C is not shown here      cLower = min_element(C.begin(), C.end());     cUpper = max_element(C.begin(), C.end());      return 0; } 

Here is the compiler error:

../MIXD.cpp:84: error: cannot convert '__gnu_cxx::__normal_iterator<double*, std::vector<double, std::allocator<double> > >' to 'double' in assignment ../MIXD.cpp:85: error: cannot convert '__gnu_cxx::__normal_iterator<double*, std::vector<double, std::allocator<double> > >' to 'double' in assignment 

What am I doing wrong?

like image 546
synaptik Avatar asked Apr 15 '12 01:04

synaptik


People also ask

What is a vector of doubles?

R Double Vector is an atomic vector whose type is “double”. A double vector can have numbers of type double or special values like NA, NaN, Inf or -Inf.

How do you remove an element from a vector in C++?

The C++ vector has many member functions. Two of these member functions are erase() and pop_back(). pop_back() removes the last element from the vector. In order to remove all the elements from the vector, using pop_back(), the pop_back() function has to be repeated the number of times there are elements.

Is std::vector on the stack?

Although std::vector can be used as a dynamic array, it can also be used as a stack.


2 Answers

min_element and max_element return iterators, not values. So you need *min_element... and *max_element....

like image 67
David Schwartz Avatar answered Oct 09 '22 13:10

David Schwartz


As others have said, std::max_element() and std::min_element() return iterators, which need to be dereferenced to obtain the value.

The advantage of returning an iterator (rather than just the value) is that it allows you to determine the position of the (first) element in the container with the maximum (or minimum) value.

For example (using C++11 for brevity):

#include <vector> #include <algorithm> #include <iostream>  int main() {     std::vector<double> v {1.0, 2.0, 3.0, 4.0, 5.0, 1.0, 2.0, 3.0, 4.0, 5.0};      auto biggest = std::max_element(std::begin(v), std::end(v));     std::cout << "Max element is " << *biggest         << " at position " << std::distance(std::begin(v), biggest) << std::endl;      auto smallest = std::min_element(std::begin(v), std::end(v));     std::cout << "min element is " << *smallest         << " at position " << std::distance(std::begin(v), smallest) << std::endl; } 

This yields:

Max element is 5 at position 4 min element is 1 at position 0 

Note:

Using std::minmax_element() as suggested in the comments above may be faster for large data sets, but may give slightly different results. The values for my example above would be the same, but the position of the "max" element would be 9 since...

If several elements are equivalent to the largest element, the iterator to the last such element is returned.

like image 38
Johnsyweb Avatar answered Oct 09 '22 11:10

Johnsyweb