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

#include <iostream>
#include <fstream>
using namespace std;

void Order(vector<int> &data){
    int count = data.size();
    int tag = false;
    for(int i = 0; i < count; i++){
        for(int j = 0; j < count - i - 1; j++){
            if(data[j] > data[j+1]){
                tag = true;
                int temp = data[j];
                data[j] = data[j+1];
                data[j+1] = temp;
            }
        }
        if(!tag) break;
    }
}

int main(){
    vector<int> data;
    ifstream in("data.txt");
    if(!in){
        cout << "file error!";
        exit(1);
    }
    int temp;
    while(!in.eof()){
        in >> temp;
        data.push_back(temp);
    }
    in.close();
    Order(data);
    ofstream out("result.txt");
    if(!out){
        cout << "file error!";
        exit(1);
    }
    for(i = 0; i < data.size(); i++) out << data[i] << " ";
    out.close();
}
原文地址:https://www.cnblogs.com/yingl/p/5817488.html