首页 > 代码库 > 文件和数组的排序

文件和数组的排序

#include <iostream>#include <fstream>#include <vector>#include <assert.h>using namespace std;template <class T>void order(vector <T>  &a){    int count = a.size();    T temp;    for (int i=0;i<count;i++)    {        for (int j=0;j<count-i-1;j++)        {            if (a[j]>a[j+1])            {                temp = a[j];                a[j] = a[j+1];                a[j+1] = temp;            }        }    }}template <class T>void order(T arr[],int len) {    T  temp;    for (int i = 0; i < len - 1; i++)        for (int j = 0; j < len - 1 - i; j++)            if (arr[j] > arr[j + 1])             {                temp = arr[j];                arr[j] = arr[j + 1];                arr[j + 1] = temp;            }}int main(void){    //文件排序    ifstream fin("order.txt");    assert(fin);    vector<double> a;    double temp;    while(!fin.eof())    {        fin>>temp;        a.push_back(temp);    }    fin.close();    order(a);    ofstream fout("order_out.txt");    assert(fout);    for (int i = 0; i<a.size(); i++)    {        fout<<a[i]<<endl;    }    //数组排序    double arr[] = { 22, 34, 3,289754282,12313,1231424214};    int len = (int) sizeof(arr) / sizeof(*arr);    order(arr,len);}

 

文件和数组的排序