Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Find minimum value in list<> using STL

Tags:

c++

stl

I have list or vector of float numbers. How to find minimum value in list/vector using STL algorithm ? I can find with iteration, but is there more elegant way to do this ?

like image 241
Damir Avatar asked Dec 21 '22 19:12

Damir


2 Answers

You can use std::min_element algorithm. Note that it won't be any faster than your iteration based algorithm, it is still O(n) complexity. But the amount of code written will be less.

like image 112
Naveen Avatar answered Dec 24 '22 01:12

Naveen


std::vector<float>::iterator iter = std::min_element(items.begin(), items.end());

std::cout << *iter << "\n";
like image 21
Jagannath Avatar answered Dec 24 '22 02:12

Jagannath