Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Why is this code running without vector header file?

I couldn’t find out why is the code below working fine on my local system without including the vector header file but not on online judges or online compilers.

#include<iostream>
#include<algorithm>

using namespace std;

int main(){
    vector<int> v(10);
    for(int i = 0; i<10; i++) v[i] = i;
    sort(v.begin(),v.end());
    for(int i = 0; i<10; i++) cout<<v[i]<<" ";
    return 0;
}

I am compiling the code by enabling the warning flags as g++ -Wall -Wextra ./ex.cpp but g++ doesn’t give me any warnings at all. Removing the #include<algorithm> does give me the error I wanted, identifier "vector" is undefined, but I don’t know what’s the relationship between them.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Your algorithm header itself includes the vector header (either directly or indirectly). Because of this, the code after the preprocessor looks the same as if you had included the vector header yourself.

You should not rely on this behavior though, as it depends on the standard library implementation you are using and can change at any time.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading