久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

如何在 C++ 中讀取和操作 CSV 文件數據?

How can I read and manipulate CSV file data in C++?(如何在 C++ 中讀取和操作 CSV 文件數據?)
本文介紹了如何在 C++ 中讀取和操作 CSV 文件數據?的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

不言自明,我試過谷歌并得到了很多可怕的專家交流,我也在這里搜索無濟于事.最好是在線教程或示例.謝謝各位.

Pretty self-explanatory, I tried google and got a lot of the dreaded expertsexchange, I searched here as well to no avail. An online tutorial or example would be best. Thanks guys.

推薦答案

如果您真正要做的是操作 CSV 文件本身,那么 Nelson 的回答是有道理的.但是,我懷疑 CSV 只是您正在解決的問題的產物.在 C++ 中,這可能意味著您的數據模型是這樣的:

If what you're really doing is manipulating a CSV file itself, Nelson's answer makes sense. However, my suspicion is that the CSV is simply an artifact of the problem you're solving. In C++, that probably means you have something like this as your data model:

struct Customer {
    int id;
    std::string first_name;
    std::string last_name;
    struct {
        std::string street;
        std::string unit;
    } address;
    char state[2];
    int zip;
};

因此,當您處理一組數據時,使用 std::vectorstd::set 是有意義的.

Thus, when you're working with a collection of data, it makes sense to have std::vector<Customer> or std::set<Customer>.

考慮到這一點,請將您的 CSV 處理視為兩個操作:

With that in mind, think of your CSV handling as two operations:

// if you wanted to go nuts, you could use a forward iterator concept for both of these
class CSVReader {
public:
    CSVReader(const std::string &inputFile);
    bool hasNextLine();
    void readNextLine(std::vector<std::string> &fields);
private:
    /* secrets */
};
class CSVWriter {
public:
    CSVWriter(const std::string &outputFile);
    void writeNextLine(const std::vector<std::string> &fields);
private:
    /* more secrets */
};
void readCustomers(CSVReader &reader, std::vector<Customer> &customers);
void writeCustomers(CSVWriter &writer, const std::vector<Customer> &customers);

一次讀取和寫入一行,而不是保留文件本身的完整內存表示.有幾個明顯的好處:

Read and write a single row at a time, rather than keeping a complete in-memory representation of the file itself. There are a few obvious benefits:

  1. 您的數據以對您的問題(客戶)有意義的形式表示,而不是當前的解決方案(CSV 文件).
  2. 您可以輕松地為其他數據格式添加適配器,例如批量 SQL 導入/導出、Excel/OO 電子表格文件,甚至 HTML 渲染.
  3. 您的內存占用可能更小(取決于相對 sizeof(Customer) 與單行中的字節數).
  4. CSVReaderCSVWriter 可以作為內存模型(例如 Nelson 的)的基礎而重復使用,而不會損失性能或功能.反之則不然.
    1. Your data is represented in a form that makes sense for your problem (customers), rather than the current solution (CSV files).
    2. You can trivially add adapters for other data formats, such as bulk SQL import/export, Excel/OO spreadsheet files, or even an HTML <table> rendering.
    3. Your memory footprint is likely to be smaller (depends on relative sizeof(Customer) vs. the number of bytes in a single row).
    4. CSVReader and CSVWriter can be reused as the basis for an in-memory model (such as Nelson's) without loss of performance or functionality. The converse is not true.

    這篇關于如何在 C++ 中讀取和操作 CSV 文件數據?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

    【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

    相關文檔推薦

    In C++ why can#39;t I write a for() loop like this: for( int i = 1, double i2 = 0; (在 C++ 中,為什么我不能像這樣編寫 for() 循環: for( int i = 1, double i2 = 0;)
    How does OpenMP handle nested loops?(OpenMP 如何處理嵌套循環?)
    Reusing thread in loop c++(在循環 C++ 中重用線程)
    Precise thread sleep needed. Max 1ms error(需要精確的線程睡眠.最大 1ms 誤差)
    Is there ever a need for a quot;do {...} while ( )quot; loop?(是否需要“do {...} while ()?環形?)
    How to break out of a loop from inside a switch?(如何從交換機內部跳出循環?)
    主站蜘蛛池模板: 欧美日韩免费视频 | 成人免费视频在线观看 | 三级视频网站 | 天天操夜夜拍 | 中文字幕在线一区二区三区 | 在线观看日韩av | 日韩精品一区二区三区在线观看 | 欧美黑人一级爽快片淫片高清 | 91在线观看 | 成人午夜激情 | 国产精品高潮呻吟久久 | 欧美日韩高清免费 | 免费视频一区二区三区在线观看 | 欧美在线激情 | 亚洲一区二区综合 | 黄色三级在线播放 | 黑人成人网| 北条麻妃国产九九九精品小说 | 先锋影音资源网站 | 99久久精品免费看国产四区 | 国产视频一区二区三区四区五区 | 国产高清一区二区 | 日韩看片 | 久久99精品久久久久久国产越南 | 国产精品久久久久不卡 | 毛片免费看 | 一区二区三区四区免费视频 | 日韩精品在线看 | av在线三级 | 日日日视频 | h视频在线看 | 蜜桃一区 | 91极品尤物在线播放国产 | 国产精品免费观看 | 天天久| 久久亚洲一区二区三区四区 | 成人一区在线观看 | 九色视频网站 | 日韩欧美专区 | 在线观看国产www | 一区二区在线 |

      <blockquote id="dalgs"><samp id="dalgs"></samp></blockquote>