方法1:使用 scanf() != EOF 去检测是否读到了文件的末尾,见函数TestOne()
方法2:使用 while(cin >> str) 读取多个string值
待续…
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <set>
#include <algorithm>
using namespace std;
// 方法1:使用 scanf() != EOF 去检测是否读到了文件的末尾,见函数TestOne()
void TestOne()
{
int count;
int tmp;
set<int> output;
while (scanf("%d", &count) != EOF)
{
for (int i = 0; i < count; ++i)
{
cin >> tmp;
output.insert(tmp);
}
for (auto iter = output.begin(); iter != output.end(); ++iter)
cout << *iter << endl;
output.clear();
}
}
// 方法2:使用 while(cin >> str) 读取多个string值
void TestTwo()
{
string str;
char ch;
stack<string> s;
while (cin >> str)
s.push(str);
while (s.size() > 0)
{
cout << s.top() << " ";
s.pop();
}
}
int main()
{
TestOne();
TestTwo();
system("pause");
return 0;
}
感谢阅读
C++文件读取与数据处理技巧
本文介绍两种在C++中读取文件的方法,包括使用scanf()检测文件末尾及利用cin读取多个字符串值。通过实例演示了如何处理输入数据,如计数、插入到集合和堆栈中,最后进行输出。
7834

被折叠的 条评论
为什么被折叠?



