<>和””之间的差异-c++

Difference between angle bracket < > and double quotes “ ” while including header files in C++?

本问题已经有最佳答案,请猛点这里访问。

Possible Duplicate:
What is the difference between #include and #include"filename"?

角括号EDOCX1 0和双引号EDOCX1?1的区别是什么,其中包括头文件在C++中?

我的意思是,哪些文件应该使用#include 来包含,哪些文件使用#include"MyFile.h"来包含????


它依赖于编译器。也就是说,通常使用"将当前工作目录中的头文件优先于系统头文件。<>通常用于系统头段。从至规范(第6.10.2节):

A preprocessing directive of the form

1
  # include <h-char-sequence> new-line

searches a sequence of implementation-defined places for a header identified uniquely by the specified sequence between the < and > delimiters, and causes the replacement of that directive by the entire contents of the header. How the places are specified or the header identified is implementation-defined.

A preprocessing directive of the form

1
  # include"q-char-sequence" new-line

causes the replacement of that directive by the entire contents of the source file identified by the specified sequence between the " delimiters. The named source file is searched for in an implementation-defined manner. If this search is not supported, or if the search fails, the directive is reprocessed as if it read

1
  # include <h-char-sequence> new-line

with the identical contained sequence (including > characters, if any) from the original
directive.

因此,在大多数编译器中,使用""首先检查本地目录,如果找不到匹配项,则继续检查系统路径。使用<>以系统头开始搜索。


使用尖括号时,编译器将在包含路径列表中搜索文件。当使用双引号时,它首先搜索当前目录(即正在编译的模块所在的目录),然后才搜索包含路径列表。

因此,按照惯例,您可以使用标准include的尖括号和其他所有内容的双引号。这样可以确保在(不推荐)情况下,如果您有一个与标准头同名的本地头,那么在每个情况下都会选择正确的头。