I'm trying to write a function that returns a pair of values from an STL container.
template <typename T>
std::pair<typename T::value_type,typename T::value_type> getMinMax(T &container) {
auto min = *(container.begin());
auto max = *(container.begin());
for (auto it : container) {
if (min > (*it) ) {
min = (*it);
}
if (max < (*it) ) {
max = (*it);
}
}
return std::make_pair(min, max);
};
int main() {
std::vector<int> c{1, 2, 3, 4, 5};
auto p = getMinMax(c);
std::cout << "min: " << p.first << " max: " << p.second << "\n";
}
I'm getting an error:
error: indirection requires pointer operand ('int' invalid) if (min > (*it) ) {
I don't know how to deal with that.
Besides that error, is there a better way to implement the desired behavior?
for range returns element, not iterator. So your loop should be something like: