直接从CSV文件获取值并作为输入传递

问题描述 投票:-1回答:1

以下是接受测试值(x和y)并提供一些输出的代码的主体

int main(void) {
    const std::vector<Tile> tiles{ Tile(0),Tile(1),Tile(2),Tile(3) };

    // Test values
    const std::vector<std::pair<double, double>> 

    test_values = {

    { 3542, 9966     },
    { 354, 11261    },
    { 354, 6932     },
    { 2354, 2432    },
    { 4354, 10002   },
    { 7354, 4932    },
    {6739, 8600     }

    };
    std::cout << "x\t"  << "y"  "\n";

    // Check cell number
    for (const Tile& tile : tiles)
    {
        for (const auto [x, y] : test_values)
        {
            if (const auto [isInTile, cellNumber] = tile.getCellNumber(x, y); isInTile) 
            {
                std::cout << x << "|\t|" << y << "\t|" << cellNumber << "\n";
            }
        }
    }

    return 0;
}

输出:

enter image description here

我想做的是更改代码,以便可以直接从csv文件获取测试值(x和y),而不是在此处键入所有内容。

我尝试过的:

//Parses through csv file line by line and returns the data in vector of vector of strings.

std::vector<std::vector<std::string> > CSVReader::getData()
{
    std::ifstream file(fileName);

    std::vector<std::vector<std::string> > dataList;

    std::string line = "";
    // Iterate through each line and split the content using delimeter
    while (getline(file, line))
    {
        std::vector<std::string> vec;
        boost::algorithm::split(vec, line, boost::is_any_of(delimeter));
        dataList.push_back(vec);
    }
    // Close the File
    file.close();

    return dataList;
}


int main(void) {
    const std::vector<Tile> tiles{ Tile(0),Tile(1),Tile(2),Tile(3) };

    // Test values
    const std::vector<std::pair<double, double>>

        test_values = {




    };

    // Creating an object of CSVWriter
    CSVReader reader("test.csv");

    // Get the data from CSV File
    std::vector<std::vector<std::string> > dataList = reader.getData();

    for (std::vector<std::string> vec : dataList)
    {
        for (std::string data : vec)
        {
            std::pair<std::string, std::string> p = ;

            std::cout << data << " ";
        }
        std::cout << std::endl;
    }


    std::cout << "x\t"  << "y"  "\n";

    // Check cell number
    for (const Tile& tile : tiles)
    {
        for (const auto [x, y] : test_values)
        {
            if (const auto [isInTile, cellNumber] = tile.getCellNumber(x, y); isInTile)
            {
                std::cout << x << "|\t|" << y << "\t|" << cellNumber << "\n";
            }

        }
    }


    return 0;
}

我无法将从csv文件读取的值传递给测试值

c++ csv input import output
1个回答
1
投票

[尝试转换dataList向量时,您要走得更远。

像现在一样遍历dataList向量,但是没有内部循环,您只需将vec[0]放入vec[1]向量中,就可以简单地得到convert the strings to doubledoubleemplace。>

也许像

test_values
© www.soinside.com 2019 - 2024. All rights reserved.