C++ C++;:使用堆栈反转文本文件中的行
到目前为止,我有以下代码:C++ C++;:使用堆栈反转文本文件中的行,c++,text,stack,reverse,lines,C++,Text,Stack,Reverse,Lines,到目前为止,我有以下代码: #include <stdio.h> #include <stdlib.h> #include <fstream> #include <iostream> #include <string> #include <stack> using namespace std; int main () { ifstream in; in.open("example.txt"); ofstream ou
#include <stdio.h>
#include <stdlib.h>
#include <fstream>
#include <iostream>
#include <string>
#include <stack>
using namespace std;
int main () {
ifstream in;
in.open("example.txt");
ofstream outfile;
outfile.open("out.txt");
stack<string> lines;
string temp;
while(getline(in, temp))
lines.push(temp);
while(!lines.empty())
outfile << lines.pop() << endl;
in.close();
outfile.close();
return 0;
}
#包括
#包括
#包括
#包括
#包括
#包括
使用名称空间std;
int main(){
如果输入;
in.open(“example.txt”);
出流孔的直径;
outfile.open(“out.txt”);
叠加线;
字符串温度;
while(getline(in,temp))
线。推(温度);
而(!lines.empty())
outfile返回void
,而不是std::string
。然后使用pop()
:
while(!lines.empty())
{
outfile返回void
,而不是std::string
。然后使用pop()
:
while(!lines.empty())
{
outfile AHHH,谢谢。所以我把它改为lines.top(),然后改为lines.pop()。啊,谢谢。所以我把它改为lines.top(),然后改为lines.pop()在那之后。额外的学分-如果你的文件有100万行,这段代码的效果如何?哈,不是家庭作业。我有一个.bmp,我把它转换成二进制,但它是颠倒的。我只需要在开始将二进制转换成十六进制之前翻转它,然后将它放入一个数组中,这样图像就可以继续显示了。小屏幕所以效率不重要。Extr一个学分-如果你的文件有100万行,这个代码工作得如何?哈,不是家庭作业。我有一个.bmp,我把它转换成二进制,但它是颠倒的。我只需要在开始将二进制转换成十六进制之前翻转它,然后把它放入一个数组中,这样图像就可以显示了。小屏幕所以效率并不重要。
while(!lines.empty())
{
outfile << lines.top() << endl;
lines.pop();
}