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

简介: 练习《C++ Primer》中的3.14节时,当敲入:#include #include using namespace std;int main(){ string word; vector text; while (cin >> word) text.

练习《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;

相关文章
|
2天前
|
算法 安全 编译器
【C++】从零开始认识泛型编程 — 模版
泛型编程是C++中十分关键的一环,泛型编程是C++编程中的一项强大功能,它通过模板提供了类型无关的代码,使得C++程序可以更加灵活和高效,极大的简便了我们编写代码的工作量。
13 3
|
3天前
|
编译器 C++ Windows
【C++】vector问题解决(非法的间接寻址,迭代器失效 , memcpy拷贝问题)
不使用memcpy函数不就可以了,然后我们使用简单粗暴的赋值拷贝,这样就不会发生浅拷贝问题了!!!
13 1
|
3天前
|
存储 算法 编译器
C++的模板与泛型编程探秘
C++的模板与泛型编程探秘
8 0
|
3天前
|
Java C++ Python
【C++从练气到飞升】06---重识类和对象(二)
【C++从练气到飞升】06---重识类和对象(二)
|
3天前
|
编译器 C++
【C++从练气到飞升】06---重识类和对象(一)
【C++从练气到飞升】06---重识类和对象(一)
|
3天前
|
存储 编译器 C语言
【C++从练气到飞升】02---初识类与对象
【C++从练气到飞升】02---初识类与对象
|
5天前
|
算法 C++ 容器
【C++/STL】vector(常见接口、模拟实现、迭代器失效)
【C++/STL】vector(常见接口、模拟实现、迭代器失效)
10 0
|
5天前
|
C++
【C++】类与对象(日期计算器)
【C++】类与对象(日期计算器)
16 0
|
5天前
|
编译器 C++
【C++】类与对象(static、explicit、友元、隐式类型转换、内部类、匿名对象)
【C++】类与对象(static、explicit、友元、隐式类型转换、内部类、匿名对象)
8 2
|
5天前
|
编译器 C++
【C++】类与对象(运算符重载、const成员、取地址重载)
【C++】类与对象(运算符重载、const成员、取地址重载)
12 2