Using Function that did not have the library included in C++

Asked

Viewed 37 times

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?

  • 1

    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...)

1 answer

1

It must be a specificity of the library implementation that is used in your system. It is possible that the library implementation vector that your compiler has access to already includes the library algorithm and everything works. Other implementations of the same library may not include so the error will occur and it will be necessary to include the library algorithm.

For example, compiling with g++ included in my Ubuntu version gives error:

$ g++ max.cpp 
max.cpp: In function ‘int main()’:
max.cpp:7:35: error: ‘max_element’ was not declared in this scope
    cout << "Maior Elemento: " << *max_element(num.begin(), num.end()) << endl;
                                   ^~~~~~~~~~~
max.cpp:7:35: note: suggested alternative: ‘max_align_t’
    cout << "Maior Elemento: " << *max_element(num.begin(), num.end()) << endl;
                                   ^~~~~~~~~~~
                                   max_align_t

In my case, I’ll have to include algorithm. To decide whether or not to include the library will depend on the case. If you have control over all the steps in the process of generating your binaries you can leave it as it is. If you’re going to distribute your source code and you don’t know who or on which platform it will be compiled, you’d better include.

If you use g++ you can see the headers included in vector. Create a file temp.c with only:

#include <vector>

So, Compile with the -M, like this:

$ g++ temp.c -M

Thus, all headers included in the library will be printed in the standard output vector.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.