どうも、ちょげ(@chogetarou)です。
Vectorの最大値を取得する方法を紹介します。
方法

Vectorの最大値を取得するには、*std::max_element()
を使います。
まず、*
を呼び出します。std::
max_element()
そして、*
の第1引数にVectorのbegin()、第2引数にVectorのend()を指定します。std::
max_element()
//vect=任意のVector
T max = *std::max_element(vect.begin(), vect.end());
上記の*
は、begin()とend()を呼び出したVectorの最大値を取得します。std::
max_element()
使用例
#include <iostream>
#include <vector>
using namespace std;
int main(void){
vector<int> num = { 1, 2, 10, 3, 4, 5 };
int max = *max_element(num.begin(), num.end());
cout << max;
}
出力:
10
コメント