#include <iostream>
#include <vector>
#include <fstream>
using namespace std;
void bubble_sort(vector<int> &array) //引用
{
int i,j,t;
for(i=0;i<array.size()-1;i++)
{
for(j=0;j<(array.size())-1-i;j++)
{
if(array[j]>array[j+1])
{
t=array[j];
array[j]=array[j+1];
array[j+1]=t;
}
}
}
}
int main()
{
vector<int> data;
ifstream in("e:\\data.txt"); //定义输入文件流对象,以输入方式打开磁盘文件e:\\data.txt,数据文件->文件流
if (!in)
{
cout<<"file error!"<<endl;
exit(1);
}
int temp;//将文件中的数据放入data容器中
while(!in.eof()) //eof文件结尾
{
in>>temp;//运用流提取运算符">>"从磁盘文件中读入整数temp,放入容器对象data中
data.push_back(temp);//在vector 对象data尾部加入一个数据
}
in.close(); //关闭磁盘文件 e:\\data.txt
bubble_sort(data); //直接以变量作为实参进行调用
ofstream out("e:\\data.txt");//定义文件流对象,打开磁盘文件e:\\result.txt
if (!out)
{
cout<<"file error!"<<endl;
exit(1);
}
for(int i=0;i<data.size();i++)//向磁盘文件 e:\\result.txt输出数据
out<<data[i]<<" ";
out.close(); //关闭磁盘文件e:\\result.txt
for(int i=0;i<data.size();i++)
cout<<data[i]<<" ";//打印
return 0;
}
源文件中有一组整数,排序后输出到另一个文件
最新推荐文章于 2021-07-26 22:55:27 发布