nkhl
nkhl

Reputation: 172

I am not able to get required output out of max_element()

I am giving input 1 2 3 4 5 to this code snippet and keep getting O as an output. I want 5(the maximum element) as the required output.

int main()
{
int inp;
std::vector<int> A;
for (int i = 0; i < 5; ++i)
{
        cin >> inp;
        A.push_back(inp);
}
int i1 = *max_element(A.begin(), A.end());
cout << A[i1];
}

Upvotes: 2

Views: 76

Answers (1)

Jarod42
Jarod42

Reputation: 217245

*max_element returns the element, not the index, so it should be:

int i1 = *std::max_element(A.begin(), A.end());
std::cout << i1;

Upvotes: 6

Related Questions