zoukankan      html  css  js  c++  java
  • *** 把一个文件中的整数排序后输出到另一个文件中

    #include <iostream>
    #include <fstream>
    #include <vector>
    using namespace std;
    
    template<class T>
    bool decreOrder(T &a, T &b)
    {
        return (a < b);
    }
    template<class T>
    bool increOrder(T &a, T &b)
    {
        return (a > b);
    }
    
    template<class T>
    void Sort(vector<T> & v, bool (*compare)(T &,T &))
    {
        int len = v.size();
        for (int i = 0; i < len-1; i++)
        {
            for (int j = len-1; j > i; j--)
            {
                if (compare(v[j-1],v[j]))
                {
                    T temp;
                    temp = v[j];
                    v[j] = v[j-1];
                    v[j-1] = temp;
                }
            }
        }
    }
    
    int main()
    {
        vector<int> v;
        ifstream in("c:\data.txt");
        if (!in)
        {
            cout << "Failed to open data.txt!" << endl;
            return 1;
        }
    
        while (!in.eof())
        {
            int temp;
            in >> temp;
            v.push_back(temp);
        }
        in.close();
    
        // sort the vector
        Sort<int>(v, increOrder<int>);
    
        // output the numbers in vector into file output.txt
        ofstream out("c:\output.txt");
        if (!out)
        {
            cout << "Failed to open output.txt!" << endl;
            return 1;
        }
    
        for (int i = 0; i < v.size(); i++)
        {
            out << v[i] << endl;
        }
    
        out.close();
        cout << "End of the program" << endl;
        return 0;
    }
  • 相关阅读:
    php防止用户输入进行跨站攻击的方式
    php中相关函数
    php运算符
    php中error_reporting
    php环境的安装
    LAMP环境介绍
    js的StringBuffer类
    一个带关闭按钮的Div窗口,很漂亮
    js  计算是今天多少周
    java 递归
  • 原文地址:https://www.cnblogs.com/superrunner/p/10182896.html
Copyright © 2011-2022 走看看