64,654
社区成员
发帖
与我相关
我的任务
分享
#include <fstream>
#include <iostream>
#include <algorithm>
#include <string>
#include <vector>
using namespace std;
int main(){
ifstream in_file("d:\\myfile.txt");
if (!in_file)
{
cerr << "oops! out_file\n" ;
return -1;
}
ofstream out_file("d:\\myfile.txt");
if (!out_file)
{
cerr << "oops! out_file\n" ;
return -2;
}
string word;
vector<string> text;
while (in_file >> word)
text.push_back (word);
int ix;
cout << "unsorted text: \n";
for (ix=0;ix<text.size();++ix)
cout << text[ix] << "\t";
cout << endl;
sort(text.begin(),text.end());
out_file << "sorted text: \n";
for (ix=1;ix<text.size();++ix)
out_file << text[ix] << "\t";
out_file << endl;
system("pause");
return 0;
}
#include <fstream>
#include <iostream>
#include <algorithm>
#include <string>
#include <vector>
using namespace std;
int main()
{
ifstream in_file("d:\\myfile.txt");
if (!in_file)
{
cerr << "oops! in_file\n" ;
return -1;
}
string word;
vector<string> text;
while (in_file >> word)
{
text.push_back(word);
}
in_file.close();
int ix;
cout << "unsorted text: \n";
for (ix=0;ix<text.size();++ix)
cout << text[ix] << "\t";
cout << endl;
sort(text.begin(),text.end());
ofstream out_file("d:\\myfile.txt");
if (!out_file)
{
cerr << "oops! out_file\n" ;
return -2;
}
out_file << "sorted text: \n";
for (ix=1;ix<text.size();++ix)
out_file << text[ix] << "\t";
out_file << endl;
out_file.close();
system("pause");
return 0;
}