《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 0;
}

程序会报错:

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 0;
}

才不会报错。

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

原文地址:https://www.cnblogs.com/weixuqin/p/6481502.html