我不明白为什么下面的代码在不包含向量头文件的情况下在我的本地系统上运行良好,但在在线法官或在线编译器上却不能。
#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;
}
Run Code Online (Sandbox Code Playgroud)
我通过启用警告标志来编译代码,g++ -Wall -Wextra ./ex.cpp但 g++ 根本没有给我任何警告。删除#include<algorithm>确实给了我我想要的错误,identifier "vector" is undefined但我不知道它们之间有什么关系。
您的算法标头本身包含向量标头(直接或间接)。因此,预处理器之后的代码看起来与您自己包含向量头一样。
不过,您不应该依赖此行为,因为它取决于您正在使用的标准库实现,并且可以随时更改。