当像"iostream“这样的库已经提供了解决方案时,你会包含"string”头吗?
示例:如果已经包含了iostream库,是否包含字符串库?哪种是正确的专业方法?
#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:如果另一个库提供功能,则使用字符串库,即使程序在没有字符串的情况下编译?
#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类编程作业中获得了分数,因为即使程序可以工作,老师说当使用字符串数据类型时,我需要包括字符串库。尽管从技术上讲,即使没有它也是可以的。这就是问题所在。
https://stackoverflow.com/questions/54936396
复制相似问题