《C++ Primer》学习笔记:向vector对象添加元素蕴含的编程假定

时间:2023-03-08 22:55:40
《C++ Primer》学习笔记:向vector对象添加元素蕴含的编程假定

练习《C++ Primer》中的3.14节时,当敲入:

#include <iostream>
#include <string> using namespace std; int main(){
string word;
vector<string> text;
while (cin >> word)
text.push_back(word);
return ;
}

程序会报错:

error: use of undeclared identifier 'vector'

其实应该插入一行:

#include <vector>

变成:

#include <iostream>
#include <string>
#include <vector> using namespace std; int main(){
string word;
vector<string> text;
while (cin >> word)
text.push_back(word);
return ;
}

才不会报错。

需要注意的是vector需要使用命名空间std,所以需要键入std::vector或在开头敲入using namespace std;