首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >什么时候在你的程序中包含<string>头?

什么时候在你的程序中包含<string>头?
EN

Stack Overflow用户
提问于 2019-03-01 08:44:19
回答 1查看 100关注 0票数 2

当像"iostream“这样的库已经提供了解决方案时,你会包含"string”头吗?

示例:如果已经包含了iostream库,是否包含字符串库?哪种是正确的专业方法?

代码语言:javascript
复制
#include <iostream>
#include <fstream>

using namespace std;
int main() {
    ifstream fin;
    fin.open("input.txt");
    string data;
    fin >> data;
    cout << data << endl; // Works with <iostream>, and without <string>
    fin.close();
    return 0;
}

示例2:如果另一个库提供功能,则使用字符串库,即使程序在没有字符串的情况下编译?

代码语言:javascript
复制
#include <iostream>
#include <string>
#include <fstream>

using namespace std;
int main() {
    ifstream fin;
    fin.open("input.txt");
    string data;
    fin >> data;       
    cout << data << endl; // Even though <iostream> allowed program to compile, we include the <string> library.
    fin.close();
    return 0;
}

从我的CSC 101类编程作业中获得了分数,因为即使程序可以工作,老师说当使用字符串数据类型时,我需要包括字符串库。尽管从技术上讲,即使没有它也是可以的。这就是问题所在。

EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/54936396

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档