"cannot open source file bits/stdc++.h" 在 Visual Studio

"cannot open source file bits/stdc++.h" in Visual Studio

#include <bits/stdc++.h>

如果我将以上行放在 program.cpp 文件的顶部,它会给我以下错误消息:

cannot open source file "bits/stdc++.h"

我该如何解决这个问题?

这不是标准 C++ 头文件,Visual C++ 没有实现它。即使您使用的编译器确实实现了它,您也不应该使用它,因为它会使您的代码立即不可移植,甚至可能在同一编译器的不同版本之间。

这是一个内部 GCC header 文件。不能保证它会在其他任何地方工作;出于多种原因,即使将它与 GCC 本身一起使用也是不好的做法。 永远不要使用它。

How can I fix this?

包括您实际需要的那些标准 header。例如,如果您需要 std::cout,则包括 <iostream>。如果您需要 std::string,则包含 <string>。如果您需要 std::ifstream,则包括 <fstream>.

由于这些是标准的 headers,它们保证在任何地方都能工作。

cppreference.com is a good free online source to find out which headers are needed for which component of the standard library. Let's take a non-obvious one, like std::ifstream. You just search for that name and you'll find http://en.cppreference.com/w/cpp/io/basic_ifstream。在那里,它说:

Defined in header <fstream>