将向量中的字符串数据解析为适当的数据类型c ++

时间:2021-11-06 01:32:56

hello all I'm reading data from a CSV file and have successfully parsed all the characters out of the data now i need to transform the string data in each column into its appropriate type (int,double,etc..). I have the following types declared that i want and then from there each variable should be updated for as many lines that come in so i can take that variable and call it somewhere else in the program. Please see Below

您好我正在从CSV文件中读取数据,并且已成功解析数据中的所有字符,现在我需要将每列中的字符串数据转换为适当的类型(int,double等等)。我声明了我想要的以下类型,然后从那里每个变量应该更新为多少行,所以我可以采取该变量并在程序中的其他地方调用它。请看下面

vector<std::vector<std::string> > matrix;
int a;
int b;
int c;
double c;
double d;
double e;
double f;
int f;
double f;
double f;
string line;
ifstream file("mynumbers - Copy.csv");

if (!file.is_open())
    perror("error while opening file");

    getline(file, line, '\n');//get the first line of columns names out

while (getline(file, line,'\n')) //find the endline charcater
    {

        vector<string> vec;
        string linevec;
        istringstream ss(line);//allows for parsing each line 

        while (getline(ss, linevec, ','))
        {

            vec.push_back(linevec);//push the parsed data for that line into a vector
        }

matrix.emplace_back(vec);//push each parsed line into another vector
        }

1 个解决方案

#1


0  

You may want to check your code for common errors. Something like this will never compile:

您可能需要检查代码是否存在常见错误。像这样的东西永远不会编译:

int c;
double c;

You also do not have a main() function

你也没有main()函数

Because of these errors this may not fit your needs exactly. Assuming each line contains three ints, four doubles, an int, and then two doubles, I would declare a "linedata" class like so:

由于这些错误,这可能不完全符合您的需求。假设每行包含三个整数,四个双精度数,一个整数,然后两个双精度数,我会声明一个“linedata”类,如下所示:

#include <iostream>  // for std::string
#include <string>    // for std::string
#include <sstream>   // for std::istringstream
#include <utility>   // for std::forward
#include <fstream>   // for std::ifstream
#include <vector>    // for std::vector
#include <stdexcept> // for std::runtime_error

class linedata
{
    int a;
    int b;
    int c;
    double d;
    double e;
    double f;
    double g;
    int h;
    double i;
    double j;

public:

    // constructs a linedata object
    linedata(std::string&& line)
    {
        std::istringstream ss( std::forward<std::string>(line) );
        char comma;
        ss >> a >> std::ws >> comma >> std::ws >> // std::ws discarding extra spaces
              b >> std::ws >> comma >> std::ws >>
              c >> std::ws >> comma >> std::ws >>
              d >> std::ws >> comma >> std::ws >>
              e >> std::ws >> comma >> std::ws >>
              f >> std::ws >> comma >> std::ws >>
              g >> std::ws >> comma >> std::ws >>
              h >> std::ws >> comma >> std::ws >>
              i >> std::ws >> comma >> std::ws >>
              j;

        // check for errors. throw if it fails to parse
        if( ss.fail() ) throw std::runtime_error("Could not parse line!");

        std::cout << a << '\n' << b << '\n' << c << '\n' << d << '\n' <<
                     e << '\n' << f << '\n' << g << '\n' << h << '\n' <<
                     i << '\n' << j << std::endl;
    }
};

Then, in main() you can try opening the file to parse the strings:

然后,在main()中,您可以尝试打开文件来解析字符串:

int main()
{
    std::vector<linedata> lines;

    std::ifstream file("mynumbers - Copy.csv");
    if( ! file.is_open() )
        // personally I would not use perror because it is part of c and 
        // requires yet another header. I'd use this:
        std::cerr << "Error occurred: " << std::strerror(errno) << std::endl;

    std::string myline;
    std::getline(file,myline); // the '\n' is not necessary. It gets a line.
                               // it already knows to look for '\n'.

    while( std::getline(file,myline) )
    {
        lines.emplace_back(std::move(myline));
    }
}

#1


0  

You may want to check your code for common errors. Something like this will never compile:

您可能需要检查代码是否存在常见错误。像这样的东西永远不会编译:

int c;
double c;

You also do not have a main() function

你也没有main()函数

Because of these errors this may not fit your needs exactly. Assuming each line contains three ints, four doubles, an int, and then two doubles, I would declare a "linedata" class like so:

由于这些错误,这可能不完全符合您的需求。假设每行包含三个整数,四个双精度数,一个整数,然后两个双精度数,我会声明一个“linedata”类,如下所示:

#include <iostream>  // for std::string
#include <string>    // for std::string
#include <sstream>   // for std::istringstream
#include <utility>   // for std::forward
#include <fstream>   // for std::ifstream
#include <vector>    // for std::vector
#include <stdexcept> // for std::runtime_error

class linedata
{
    int a;
    int b;
    int c;
    double d;
    double e;
    double f;
    double g;
    int h;
    double i;
    double j;

public:

    // constructs a linedata object
    linedata(std::string&& line)
    {
        std::istringstream ss( std::forward<std::string>(line) );
        char comma;
        ss >> a >> std::ws >> comma >> std::ws >> // std::ws discarding extra spaces
              b >> std::ws >> comma >> std::ws >>
              c >> std::ws >> comma >> std::ws >>
              d >> std::ws >> comma >> std::ws >>
              e >> std::ws >> comma >> std::ws >>
              f >> std::ws >> comma >> std::ws >>
              g >> std::ws >> comma >> std::ws >>
              h >> std::ws >> comma >> std::ws >>
              i >> std::ws >> comma >> std::ws >>
              j;

        // check for errors. throw if it fails to parse
        if( ss.fail() ) throw std::runtime_error("Could not parse line!");

        std::cout << a << '\n' << b << '\n' << c << '\n' << d << '\n' <<
                     e << '\n' << f << '\n' << g << '\n' << h << '\n' <<
                     i << '\n' << j << std::endl;
    }
};

Then, in main() you can try opening the file to parse the strings:

然后,在main()中,您可以尝试打开文件来解析字符串:

int main()
{
    std::vector<linedata> lines;

    std::ifstream file("mynumbers - Copy.csv");
    if( ! file.is_open() )
        // personally I would not use perror because it is part of c and 
        // requires yet another header. I'd use this:
        std::cerr << "Error occurred: " << std::strerror(errno) << std::endl;

    std::string myline;
    std::getline(file,myline); // the '\n' is not necessary. It gets a line.
                               // it already knows to look for '\n'.

    while( std::getline(file,myline) )
    {
        lines.emplace_back(std::move(myline));
    }
}