I am trying to find the minimal element of a subset of an array in c++ as follows:
#include <iostream>
#include <algorithm>
using std::cin;
using std::cout;
using std::min_element;
void load_values(int n);
unsigned *w;
int main() {
int n, a, b;
cin >> n >> a >> b;
w = new unsigned[n];
load_values(n); // sets values to w[i]
int min = min_element(w+a, w+b);
}
void load_values(int n) {
while(n--)
w[n] = 1;
}
I get the error invalid conversion from 'unsigned int*' to 'unsigned int' [-fpermissive]
. What am I missing here?
Note, that
std::min_element()
returns an iterator to a minimal value, not the value itself. I doubt that you get this error based on the above code alone: assuming proper include directives and using directives the code compiles OK although it would print the address of the minimum element rather than its value. Most likely your actual code looks more likeNote, that you should also check the result of reading from
std::cin
:... and, of course, it seems you're leaking the memory allocated for
w
.