您的位置:首页 > 其它

文件中有一组整数,要求排序后输出到另一个文件中

2012-08-27 15:55 363 查看
#include <iostream>
#include <vector>
#include <fstream>

using namespace std;
void BubbleSort(vector<int>& array)
{
for (int i=0;i!=array.size();i++)
{
for (int j=array.size()-1;j!=i;j--)
{
if (array[j]<array[j-1])
{
swap(array[j],array[j-1]);
}
}
}
}

void swap(int* a,int* b)
{
int temp;
temp=*a;
*a=*b;
*b=temp;
}
void main()
{
vector<int> data;
ifstream in("c:\\data.txt");
if (!in)
{
cout<<"file error!"<<endl;
exit(1);
}
int temp;
while(!in.eof())
{
in>>temp;
data.push_back(temp);
}
in.close();
BubbleSort(data);
ofstream out("c:\\result.txt");
if (!out)
{
cout<<"file error!"<<endl;
exit(1);
}
for(int i=0;i<data.size();i++)
out<<data[i]<<"  ";
out.close();
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐