• 榕树贷款C++ STL 中没有内置 CSV 读取器/写入器


    C++读写CSV文件
    据我所知,C++ STL 中没有内置 CSV 读取器/写入器。这不是对 C++ 的打击。榕树贷款只是一种低级语言。如果我们想用 C++ 读写 CSV 文件,榕树贷款将不得不处理文件 I/O、数据类型和一些关于如何读取、解析和写入数据的低级逻辑。对我来说,这是构建和测试更多有趣程序(如机器学习模型)的必要步骤。

    榕树贷款写入CSV
    这里,ofstream是一个“输出文件流”。由于它派生自ostream,我们可以像对待cout一样对待它(它也是派生自ostream)。执行这个程序的结果是,榕树贷款在可执行文件所在的目录中得到了一个名为ones.csv的文件。让我们将它封装到write_csv()函数中。

    #include <string>
    #include <fstream>
    #include <vector>

    void write_csv(std::string filename, std::string colname, std::vector<int> vals){
        // Make a CSV file with one column of integer values
        // filename - the name of the file
        // colname - the name of the one and only column
        // vals - an integer vector of values
        
        // Create an output filestream object
        std::ofstream myFile(filename);
        
        // Send the column name to the stream
        myFile << colname << "\n";
        
        // Send data to the stream
        for(int i = 0; i < vals.size(); ++i)
        {
            myFile << vals.at(i) << "\n";
        }
        
        // Close the file
        myFile.close();
    }

    int main() {
        // Make a vector of length 100 filled with 1s
        std::vector<int> vec(100, 1);
        
        // Write the vector to CSV
        write_csv("ones.csv", "Col1", vec);
        
        return 0;

  • 相关阅读:
    leetcode 637. Average of Levels in Binary Tree 二叉树的层平均值(简单)
    C语言链式栈
    项目管理之人力资源管理
    前端程序员接私活,直呼赚麻了
    PHP反序列化
    day7【代码随想录】移除链表元素
    Oracle中查询表结构的六种方法
    039-第三代软件开发-PDF阅读器
    SimpleAdapter和RecyclerView纪录
    浅谈 Spring
  • 原文地址:https://blog.csdn.net/dongsijia/article/details/125621804