zoukankan      html  css  js  c++  java
  • 数据读取

    #include <iostream>
    #include <iterator>
    #include <vector>
    #include <cstddef>
    #include <string>
    #include <sstream>
    #include <fstream>
    #include <algorithm>
    #include <cmath>
    #include <set>
    #include <limits>
    #include <functional>
    #include <numeric>
    
    void ReadDataFromCsv(const std::string &filename, std::vector<std::vector<std::string> > &lines_feat) {
        std::ifstream vm_info(filename.c_str());
        std::string lines, var;
        std::vector<std::string> row;
    
        lines_feat.clear();
    
        while(!vm_info.eof()) {
            getline(vm_info, lines);
            if(lines.empty())
                break;
            std::istringstream stringin(lines);
            row.clear();
    
            while(std::getline(stringin, var, ',')) {
                row.push_back(var);
            }
            lines_feat.push_back(row);
        }
    }
    
    template <class DataType>
    void ReadMatFromFile(std::string &filename, std::vector<std::vector<DataType> > &lines_feat) {
        std::ifstream vm_info(filename.c_str());
        std::string lines;
        DataType var;
        std::vector<DataType> row;
    
        lines_feat.clear();
    
        while(!vm_info.eof()) {
            getline(vm_info, lines);
            if(lines.empty())
                break;
            std::replace(lines.begin(), lines.end(), ',', ' ');
            std::stringstream stringin(lines);
            row.clear();
    
            while(stringin >> var) {
                row.push_back(var);
            }
            lines_feat.push_back(row);
        }
    }
    
    void ReadStringFromFile(std::string &filename, std::vector<std::string> &in_string) {
        std::ifstream vm_info(filename.c_str());
        std::string lines, var;
    
        while(!vm_info.eof()) {
            getline(vm_info, lines);
            if(lines.empty())
                break;
            std::stringstream stringin(lines);
    
            while(stringin >> var) {
                in_string.push_back(var);
            }
        }
    }
    
    void ReadDataFromFile(std::string &filename, std::vector<std::string> &lines_feat) {
        std::ifstream vm_info(filename.c_str());
        std::string lines;
    
        lines_feat.clear();
    
        while(!vm_info.eof()) {
            getline(vm_info, lines);
            if(lines.empty())
                break;
    
            lines_feat.push_back(lines);
        }
    }
    
    void ReadSetFromFile(std::string &filename, std::set<std::string> &lines_feat) {
        std::ifstream vm_info(filename.c_str());
        std::string lines;
    
        lines_feat.clear();
    
        while(!vm_info.eof()) {
            getline(vm_info, lines);
            if(lines.empty())
                break;
    
            lines_feat.insert(lines);
        }
    }
    
    void ProcessSet(std::set<std::string> &s1, std::set<std::string> &s2) {
        std::set<std::string>::const_iterator it=s1.begin(), find_it;
    
        for(; it!=s1.end(); ++it) {
            find_it=s2.find(*it);
            if(find_it == s2.end()) {
                std::cout<<*it<<", not found.
    ";
            }
        }
    }
    
    std::string lowerCase(const std::string& s) {
        std::string lower(s); 
        for(size_t i=0;i<s.length();++i) {
            lower[i]=tolower(lower[i]);
        }
        return lower;
    }
    
    std::string letters(const std::string& s) {
        std::string letter;
        for(size_t i=0;i<s.length();++i) {
            char ch=s.at(i);
            bool flag=false;
            if((ch>=65 && ch<=90)) {
                ch=ch+32;
                flag=true;
            } 
            else if((ch>=97 && ch<=122) || (ch>=48 && ch<=57)) {
                flag=true;
            } 
            else {
                ;
            }
            if(flag) {
                letter.push_back(ch);
            }
        }
        letter.push_back('');
        return letter;
    }
    
    template <class T1, class T2>
    int MatMultiply(const std::vector<std::vector<T1> > &Mata, const std::vector<std::vector<T2> > &Matb, std::vector<std::vector<T1> > &MatOut) {
        if(Mata.at(0).size() != Matb.size()) {
            std::cout<<"not match!
    ";
            return -1;
        }
        for(size_t i=0; i<Mata.size(); ++i) {
            for(size_t j=0; j<Matb.at(0).size(); ++j) {
                std::vector<T2> col;
                col.clear();
                for(size_t k=0; k<Matb.size(); ++k) {
                    col.push_back(Matb.at(k).at(j));
                }
                MatOut.at(i).at(j)=inner_product(Mata.at(i).begin(), Mata.at(i).end(), col.begin(), 0);
            }
        }
        return 0;
    }
    
    template <class T1, class T2, class T3>
    void outer_product(const std::vector<T1> &inst1, const std::vector<T2> &inst2, std::vector<std::vector<T3> > &out) {
        std::vector<T3> temp_row(inst2.size());
    
        for(typename::std::vector<T1>::const_iterator it=inst1.begin();it!=inst1.end();++it) {
            transform(inst2.begin(), inst2.end(), temp_row.begin(), bind2nd(std::multiplies<T1>(), *it));
            out.push_back(temp_row);
        }
    }
    
    std::vector<std::string> split(const std::string& s, char delimiter) {
        std::vector<std::string> tokens;
        std::string token;
        std::istringstream tokenStream(s);
    
        while(std::getline(tokenStream, token, delimiter)) {
            tokens.push_back(token);
        }
        return tokens;
    }
    
    int stringtoint(const std::string& s) {
        std::istringstream iss(s);
        int num;
        return iss>>num?num:0;
    }
    
    void printip(const std::string& s) {
        std::vector<std::string> temp, ip_segment;
    
        temp=split(s, '-');
        ip_segment=split(temp.front(), '.');
    
        std::string ip_start=ip_segment.back(), ip_end=temp.back();
        int start, end;
        start=stringtoint(ip_start);
        end=stringtoint(ip_end);
    
        for(size_t i=start;i<=end;++i) {
            std::cout<<ip_segment[0]<<"."<<ip_segment[1]<<"."<<ip_segment[2]<<"."<<i<<"
    ";
        }
    
    }
    
    char process_field(const std::string& field) {
        std::istringstream iss(field);
        std::vector<std::string> results((std::istream_iterator<std::string>(iss)), std::istream_iterator<std::string>());
    
        return results.back().at(0);
    }
    
    void Process2DVector(const std::vector<std::vector<std::string> > &vv) {
        std::ofstream fsa("/tmp/6system", std::ios::trunc);
        std::ofstream fsb("/tmp/7system", std::ios::trunc);
        for(size_t i=0; i<vv.size(); ++i) {
            char flag=process_field(vv.at(i).back());
            if(flag=='7')
                fsb<<vv.at(i).front()<<"
    ";
            else if(flag=='6')
                fsa<<vv.at(i).front()<<"
    ";
            else
                ;
        }
    }
    
    template <class T>
    void Display2DVector(const std::vector<std::vector<T> > &vv) {
        for(size_t i=0;i<vv.size();++i) {
            for(typename::std::vector<T>::const_iterator it=vv.at(i).begin();it!=vv.at(i).end();++it) {
                std::cout<<*it<<" ";
            }
            std::cout<<"
    ";
        }
        std::cout<<"--------the total of the 2DVector is "<<vv.size()<<std::endl;
    }
    
    int main() {
        std::string file_path("hostinfo20190703.csv");
        std::vector<std::vector<std::string> > vv_dat;
    
        ReadDataFromCsv(file_path, vv_dat);    
    
        //Display2DVector(vv_dat);
        
        Process2DVector(vv_dat);
    
        return 0;
    }
  • 相关阅读:
    12.浏览器测试
    11.测试用例管理
    10.测试用例的钩子
    如何处理JSON中的特殊字符
    foreach的参数不是数组:Warning: Invalid argument supplied for foreach
    CI中的控制器中要用model中的方法,是统一写在构造器方法中,还是在每一个方法中分别写
    CodeIgniter配置之config
    **Apache Options指令详解
    .htaccess的基本作用及相关语法介绍
    .htaccess文件的作用(访问控制)
  • 原文地址:https://www.cnblogs.com/donggongdechen/p/11128917.html
Copyright © 2011-2022 走看看