使用模板时读取CSV文件

时间:2018-04-06 00:32:34

标签: c++

我是C++的新手,所以我要做的是在我的函数readRecord(CSV模板化函数)中读取readRecord()文件。在我的CSV文件中,我存储了一些数据,例如年份和犯罪率,犯罪率等,并使用显示功能显示它。我到目前为止发布了我的代码但它给了我错误我不知道如何让它工作。请有人帮帮我吗?

#ifndef SICT_DATA_H
#define SICT_DATA_H
#include <fstream>
#include <iomanip>

namespace sict {


    const int LARGEST_NUMBER = 1000000000; // 1,000,000,000
    const int SMALLEST_NUMBER = 0;
    const int COLUMN_WIDTH = 15;

    template <typename T>

    T max(const T* data, int n)
    {
        int maximum = data[0];

        for (int i = 0; i < n; i++)
        {
            if (data[i] > maximum)
            {
                maximum = data[i];
                //*data = maximum;
            }
        }
        return maximum;
    }

    template <typename T>

    T min(const T* data, int n)
    {
        T minimum = data[0];

        for (int i = 0; i < n; i++)
        {
            if ((dataa[i] < minimum) && (data[i] >= SMALLEST_NUMBER))
            {
                minimum = data[i];
                //*data = minimum;
            }
        }
        return minimum;
    }

    template <typename T>

    T sum(const T* data, int n)
    {
        double sum;
        for (int i = 0; i <= n; i++)
        {
            sum = sum + data[i];
        }

        return sum;
    }

    template <typename T>

    double average(const T* data, int n)
    {
        double average;
        for (int i = 0; i <= n; i++)
        {
            average = (sum(data, n) / n);
        }
        return average;
    }

    template <typename T>

    bool read(std::istream& input, T* data, int n)
    {
        char comma;
        for (int i = 0; i < n; i++)
        {
            input >> comma >> data[i];

            if (cin.fail())
            {
                return false;
            }

        }
        return true;
    }

    template <typename T>

    void readRecord()
    {


        ifstream ip("crimedata.csv");
        int year[5];
        int pop[5];
        int crime[5];
        int rate[5];
        int autoo[5];
        int rate1[5];
        while (ip.EOF())
        {
            for (int i = 0; i < 5 i++)
            {
                getline(ip.year[i], ',');
                getline(ip.pop[i], ',');
                getline(ip.crime[i], ',');
                getline(ip.rate[i], ',');
                getline(ip.autoo[i], ',');
                getline(ip.rate1[i], '\n');
                cout << year[i] << pop[i] << crime[i] << endl;
            }


        }
        ip.close;

    }


        template <typename T>

        void display(const char* name, const T* data, int n)
        {
            readRecord();
            cout << setw(20)  << name   ;
            cout << setw(15) << data   << endl;


        }
}

#endif

Csv文件:

5
Year 2000 2001 2002 2003 2004 Population 281421906 285317559 287973924 290788976 293656842 ViolentCrime 1425486 1439480 1423677 1383676 1360088 ViolentCrime_Rate 506.5 504.5 494.4 475.8 463.2 GrandTheftAuto 1160002 1228391 1246646 1261226 1237851 GrandTheftAuto_Rate 412.2 430.5 432.9 433.7 421.5

0 个答案:

没有答案