0
I was reading about the function max_element
in the documentation of C++ which is a function that points to the largest element in a list, vector, etc... And there it said that this function belongs to Algorithm library
then I went to test. The code looked like this:
#include <iostream>
#include <vector>
using namespace std;
int main() {
vector <int> num = {3, 6, 1, 4};
cout << "Maior Elemento: " << *max_element(num.begin(), num.end()) << endl;
return 0;
}
He executed it the right way, showed the largest element of the vector that in the case was 6.
But I noticed you forgot to include the library algorithm
which is where the function max_element
belonging.
But then why was there no error if I didn’t even include the library of that function I was using? Some bug in the compiler?
Never assume the bug is in the compiler (rarely). Think about it: the C++ compiler has been used for decades by thousands of professionals to make highly complex programs. What is the chance of a simple program causing a bug that has gone unnoticed by all these professionals? I’m not saying it’s impossible, but it is much unlikely. Most likely is what was answered down there (or some other simpler problem, either in the program, or in the form you compiled). Finding "compiler bug" should never be the first option (neither the second nor the hundredth...)
– hkotsubo