【发布时间】:2019-10-09 12:19:13
【问题描述】:
我一直在学习面向对象的计算,尤其是迭代器和标准模板库等。
如果你这么写,我似乎不太明白为什么
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; // this is a using directive telling the compiler to check the std namespace when resolving identifiers with no prefix-- learncpp.com/cpp-tutorial/… -
在所有种情况下,您需要使用
#include <vector>,而不仅仅是因为它不能编译。另一个标题可能包含它,但不能保证。 -
std:vector<int>一个分号是label。