#include <bits/stdc++.h>
using namespace std;
int main()
{
int b;
vector<int>={32,3,4,34,32,5465,6543}
b=d[*max_element(d.begin(),d.end())];
cout << b;
}
why won't this code print the max element of the vector d.
#include <bits/stdc++.h>
using namespace std;
int main()
{
int b;
vector<int>={32,3,4,34,32,5465,6543}
b=d[*max_element(d.begin(),d.end())];
cout << b;
}
why won't this code print the max element of the vector d.
Because what std::max_element()
returns is an iterator. To access the element you can dereference it via *
, which is what you do, but instead of printing that maximum value, you use it to index the vector.
Simply output it: std::cout << *std::max_element(d.begin(),d.end());
.