リストから最大/最小の要素を検索
サンプルコード
#include <iostream>
#include <algorithm>
#include <vector>
int main()
{
std::vector<int> v = { 3, 1, 4, 1, 5, 9, 2 };
std::vector<int>::const_iterator p1;
p1 = std::min_element(v.begin(), v.end());
std::cout << "min: " << "v[" << static_cast<int>(p1 - v.begin()) << "] = " << *p1 << std::endl;
p1 = std::max_element(v.begin(), v.end());
std::cout << "max: " << "v[" << static_cast<int>(p1 - v.begin()) << "] = " << *p1 << std::endl;
auto p2 = std::minmax_element(v.begin(), v.end());
std::cout << "min: " << *p2.first << ", max: " << *p2.second << std::endl;
return 0;
}
実行結果
min: v[1] = 1
max: v[5] = 9
min: 1, max: 9