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