如果你已经包含了 using name space std,为什么你不必#include<vector>?

Why does dont you have to #include<vector>, if you already include using name space std?

我一直在学习面向对象的计算,尤其是迭代器和标准模板库等。

我似乎不太明白为什么要写

std:vector<int> - //blah, a vector is created.

但是,在某些情况下你需要写

#include <vector> //to include vector library

这是为什么? 我们通常写的标准库"using namespace std" - 是否已经包含矢量库?

当我删除定义文件#include 时,计算机无法识别我的矢量变量。

但是,我看到在某些情况下,许多人使用向量函数而没有实际声明它 std::vector???

std::vector<int>::iterator pos;
std::vector<int>coll;

这是其他人使用的代码,似乎有效?

#include <iostream>
#include <algorithm>
#include <iterator>
#include <vector>

using namespace std;

int main() {
vector<int>::iterator pos;
vector<int>coll;
}

// 这对我有用,但我想了解为什么这个有效而另一个无效。

using namespace std; 指令只是说“对于 std 命名空间 中我知道的任何东西 ,你可以省略 std::字首”。但是如果没有 #include <vector>(直接或间接通过其他 #include),编译器根本不知道 std::vector 存在。

Headers 为您提供各种 类 和 API 的声明(在某些情况下,还有定义); using namespace 声明只是消除了使用命名空间前缀明确限定对它们的引用的需要。

您仍然需要执行 #includes 的原因是关于消除声明的冲突(您不想只包括所有可能的包含文件,因为其中一些可能与某些定义冲突名称)和编译性能(#include 世界意味着很多千字节,如果不是兆字节,额外的代码要编译,其中绝大多数你不会实际使用;限制它到 headers您实际上需要更少的磁盘 I/O、更少的内存和更少的 CPU 时间来执行编译)。

为了将来参考,“我们通常写 'using namespace std;' 的地方”表示您已经养成了坏习惯。 using namespace std; is frowned upon in production code.

#include <some_file> 只是将 #include 指令替换为文件 "some_file".

的内容

#include <vector> 的情况下,文件 "vector" 包含 std 命名空间中 class 模板 vector 的定义。要声明 class 的实例,class 的定义必须对编译器可见,因此要声明 std::vector<int> foo,您必须 #include <vector>,直接或间接通过 #include正在 #include 创建另一个文件。

那么为什么有些代码示例必须 #include <vector> 而有些则不需要?好吧,他们都这样做。有些人可能不会 #include 明确地,但他们必须 #include 另一个执行 #include <vector>.

的文件

当然,在许多示例片段中,为了简洁起见,人们会简单地省略 #include。如果没有适当的 #includes,代码将无法编译,但它们会使代码更长,并且可能会分散作者试图表达的观点。


语句 using namespace std; 只是告诉编译器它应该在 std 命名空间中搜索它在当前命名空间中找不到的任何非限定名称。这意味着编译器可以通过名称 vectorstd::vector.

找到 vector class

向量是 C++ STL 的一部分,是的,但是您需要包含 <vector> header 文件才能使用它。如果您想使用 STL 字符串,这与您需要 #include <string> 的原因相同。在包含这些 header 之后,您需要在声明向量或字符串之前使用 std:: 命名空间标识符,以便编译器知道您声明的是 C++ STL 的一部分,而不是 built-in 到 "base" C 或 C++。

基本上,using namespace std 的作用是让您能够在 stringvector 或其他任何内容之前删除 std:: 限定词。不推荐这样做,here's 一篇文章简要解释了为什么你不应该使用它。相反,如果您想清理代码并且不想每次打印到控制台时都键入 std::,那么请考虑导入单个标识符,例如 using namespace std::cout

希望这对您有所帮助。

Why does dont you have to #include, if you already include using name space std?

仅当您使用 header 中的声明时,才必须包含 <vector>。否则你不需要包括它。你永远需要 using namespace std;.

don't quite seem to understand why if you write

std:vector<int> - //blah, a vector is created.

However, in some cases you need to write

#include <vector> //to include vector library

如果您创建 std:vector<int> objects.

,您 总是 必须包括 <vector>

Does the standard library where we usually write "using namespace std" - already include the vector library?

不,标准库不使用 using namespace std;。如果是这样,那将使使用 std 命名空间的全部意义无效。

When I remove the definition file #include, then the computer cannot recognize my vector variables.

这是因为您不能声明尚未定义的类型的变量。 std::vector的定义在<vector>

首先,std:vector<int> - //blah, a vector is created. 不会编译,因为您使用了单个冒号 :,这意味着 std 这里是一个 label 声明。并且标签名称不能是 vector<int>。所以我猜你的意思是:std::vector<int>.

其次,如您所知,vectoriostream、...分别是headers、<vector><iotream>中定义的库类型。 . 要使用这些类型,您必须首先直接或间接包含这些 headers。

namespace std 在 C++ 中是一组标识符,用于减少命名冲突的可能性。在现代 C++ 中,C++ 标准库中的所有功能现在都定义在命名空间 std(标准的缩写)中。

例如 std::cout;此标识符在类型为 ostream 的命名空间 std 中声明。所以你必须首先包含 iostream 以便编译器可以看到 ostream object 是什么,然后告诉编译器这个 object 在哪里声明? in namespace std。因此,仅包含 iostream 是不够的,因此您要么添加声明 cout 的命名空间 std 的全部内容,要么直接告诉编译器明确声明 cout 的位置完全符合条件:

#include <iostream> // contains output/input library types definitions
using std::cout; // ok add only cout definition to the program not the whole content of std.
using namespace std; // contains cout, cin, vector....

Why don't you have to include < vector > if you already include using namespace std

看看这个url:https://en.cppreference.com/w/cpp/header

有 100 多个 header 文件可供您使用。

恕我直言,您感到困惑的是,header 作者也可以使用这 100 多个 header,并且他们还可以访问 header 通常不发表的文章在标准中。结果是,例如,当您或我包含 时,该包含的某些间接部分也可能 'pull-in'

我建议您不要在代码中加入 "using namespace std"。它的使用并没有故意导致 的 'hidden / indirect' 包含,并且可能不会在下一个实现中出现。

我正在使用 g++v7.3。我很快就会升级到当前的 g++(我认为 9.x?)你不能依赖包含 除非你明确包含它。

this works for me, but I want to understand why this one works and the other one doesn't.

运气好...我觉得很糟糕,如果你因此养成了多个坏习惯。


如果你的编译器支持 -std=c++17 或更好,它有一个我喜欢的新特性。新功能允许我在 header 包含之后立即指定我特别需要的库中的哪个函数。它看起来像这样:

#include <iostream>
using std::cout, std::cerr, std::endl, std::flush,
      std::hex, std::dec, std::cin;

#include <iomanip>
using std::setw, std::setfill;

#include <string>
using std::string, std::to_string;

#include <thread>
using std::thread, std::this_thread::sleep_for;

#include <vector>
using std::vector;

您自己的库也可以类似处理:

#ifndef                 DTB_ENG_FORMAT_HH
#include "../../bag/src/dtb_eng_format.hh"
using DTB::EngFormat_t;
#endif

#ifndef                 DTB_PPLSEM_HH
#include "../../bag/src/dtb_pplsem.hh"
using DTB::PPLSem_t;
#endif

#ifndef                 DTB_ENG_FORMAT_HH
#include "../../bag/src/dtb_eng_format.hh"
#endif

#ifndef                 DTB_ASSERT_HH
#include "../../bag/src/dtb_assert.hh"
#endif

我试着跟踪其中的一小部分,并将它们收集在一个文件中。当我开始一项新的工作时,我会使用更大的列表,并简单地删除 'unused' 函数(当我想 post 我的工作时)。