久久久久久久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?(如何從交換機內部跳出循環?)
    主站蜘蛛池模板: 欧美伦理一区二区 | 日韩美女在线 | 欧美香蕉视频 | 免费一级毛片免费播放 | 欧美xxxx网站 | 毛片www| 九九精品在线观看 | www一区二区 | 国产成人小视频 | 在线看的av| 狠狠干| 黄色免费小视频 | 国产精品国产三级国产专区52 | 欧美精品三区 | 亚洲第一区视频 | 欧美一级特黄视频 | 婷婷丁香激情 | 五月亚洲 | 精品一区二区三区视频 | 美女无遮挡网站 | 中文字幕在线播放视频 | 香蕉视频久久 | 国产乱码一区二区三区 | 精品欧美黑人一区二区三区 | 久热精品视频 | 在线观看h视频 | av片在线观看 | 欧美在线免费 | 18视频在线观看男男 | 成人在线一区二区 | 黄色福利 | 久久最新视频 | 国产精品美女久久久 | 国产成人精品一区二区三区在线 | 免费国产黄色 | 黄色小视频免费看 | 国产黄色免费网站 | 黄色片网站视频 | 欧美一级在线观看 | 青青草av | 亚洲视频在线看 |
    <code id="qoags"><cite id="qoags"></cite></code>
      • <dl id="qoags"><sup id="qoags"></sup></dl>