C++ 你为什么不包括<;向量>;,如果已经包括使用名称空间std?

C++ 你为什么不包括<;向量>;,如果已经包括使用名称空间std?,c++,vector,stl,stdvector,C++,Vector,Stl,Stdvector,我一直在学习面向对象的计算,特别是迭代器和标准模板库等 我似乎不太明白你为什么要写信 std:vector<int> - //blah, a vector is created. std:vector<int> - //blah, a vector is created. std:vector-//诸如此类,创建了一个向量。 但是,在某些情况下,您需要编写 #include <vector> //to include vector library #i

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

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

std:vector<int> - //blah, a vector is created.
std:vector<int> - //blah, a vector is created.
std:vector-//诸如此类,创建了一个向量。
但是,在某些情况下,您需要编写

#include <vector> //to include vector library
#include <vector> //to include vector library
#include//包含向量库
为什么会这样? 我们通常编写“使用名称空间std”的标准库是否已经包含向量库

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

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

std::vector<int>::iterator pos;
std::vector<int>coll;
std::vector::iterator pos;
维克托科尔;
这是其他人使用的代码,它似乎有效吗

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

using namespace std;

int main() {
vector<int>::iterator pos;
vector<int>coll;
}
#包括
#包括
#包括
#包括
使用名称空间std;
int main(){
向量::迭代器位置;
vectorcoll;
}

//这对我来说是可行的,但我想了解为什么这一个有效而另一个无效。

使用名称空间std的
指令只是说“对于
std
名称空间中我所知道的任何内容,您可以去掉
std::
前缀”。但是如果没有
#include
(直接或间接通过其他
#include
),编译器一开始就不知道
std::vector
的存在

标头为您提供了各种类和API的声明(在某些情况下还包括定义);
usingnamespace
声明只是消除了使用名称空间前缀显式限定对它们的引用的需要

您仍然需要执行
#include
的原因是关于声明的反冲突(您不希望只包含每个可能的include文件,因为其中一些文件可能对某些名称有冲突的定义),以及编译性能(
#include
ing the world意味着要编译的额外代码如果不是兆字节的话,也要有很多千字节,其中绝大多数您不会实际使用;将其限制在您实际需要的头文件中意味着更少的磁盘I/O、更少的内存和更少的CPU时间来执行编译)


作为将来的参考,“我们通常编写‘使用名称空间std;’”表示您已经学会了坏习惯。
使用名称空间std;

使用名称空间std;
指令只是说“对于
std
名称空间中我知道的任何内容,您可以省去
std:
前缀”但是如果没有
#include
(直接或间接通过其他
#include
),编译器一开始就不知道
std::vector
的存在

头为您提供了各种类和API的声明(在某些情况下还提供了定义);使用名称空间的
声明消除了使用名称空间前缀显式限定对它们的引用的需要

您仍然需要执行
#include
的原因是关于声明的反冲突(您不希望只包含每个可能的include文件,因为其中一些文件可能对某些名称有冲突的定义),以及编译性能(
#include
ing the world意味着要编译的额外代码如果不是兆字节的话,也要有很多千字节,其中绝大多数您不会实际使用;将其限制在您实际需要的头文件中意味着更少的磁盘I/O、更少的内存和更少的CPU时间来执行编译)

作为将来的参考,“我们通常编写‘使用名称空间std;’”表示您已经学会了坏习惯。
使用名称空间std;

#include
只是将
#include
指令替换为文件“some_file”的内容

对于
#include
,文件“vector”包含
std
命名空间中类模板
vector
的定义。若要声明类的实例,该类的定义必须对编译器可见,因此若要声明
std::vector foo
,您必须
#包含
,可以通过
#包含
直接或间接包含另一个文件ode>#包括它

那么,为什么有些代码示例必须
#包含
#而另一些则不包含
#呢?好吧,它们都是这样做的。有些代码示例可能不明确地包含
,但它们必须
#包含
另一个文件

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


使用命名空间std;的语句
只告诉编译器应该在
std
命名空间中搜索在当前命名空间中找不到的任何非限定名称。这意味着编译器可以通过名称
vector
std::vector
找到
vector
类de
只是将
#include
指令替换为文件“some_file”的内容

对于
#include
,文件“vector”包含
std
命名空间中类模板
vector
的定义。若要声明类的实例,该类的定义必须对编译器可见,因此若要声明
std::vector foo
,您必须
#包含
,可以通过
#包含
直接或间接包含另一个文件ode>#包括它

那么,为什么有些代码示例必须
#包含
#而另一些则不包含
#呢?好吧,它们都是这样做的。有些代码示例可能不明确地包含
,但它们必须
#包含
另一个文件

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