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

如何在 C++ 中讀取和操作 CSV 文件數(shù)據(jù)?

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

問題描述

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

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 只是您正在解決的問題的產(chǎn)物.在 C++ 中,這可能意味著您的數(shù)據(jù)模型是這樣的:

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;
};

因此,當(dāng)您處理一組數(shù)據(jù)時(shí),使用 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>.

考慮到這一點(diǎn),請(qǐng)將您的 CSV 處理視為兩個(gè)操作:

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);

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

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. 您的數(shù)據(jù)以對(duì)您的問題(客戶)有意義的形式表示,而不是當(dāng)前的解決方案(CSV 文件).
  2. 您可以輕松地為其他數(shù)據(jù)格式添加適配器,例如批量 SQL 導(dǎo)入/導(dǎo)出、Excel/OO 電子表格文件,甚至 HTML 渲染.
  3. 您的內(nèi)存占用可能更小(取決于相對(duì) sizeof(Customer) 與單行中的字節(jié)數(shù)).
  4. CSVReaderCSVWriter 可以作為內(nèi)存模型(例如 Nelson 的)的基礎(chǔ)而重復(fù)使用,而不會(huì)損失性能或功能.反之則不然.
    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.

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

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

    相關(guān)文檔推薦

    In C++ why can#39;t I write a for() loop like this: for( int i = 1, double i2 = 0; (在 C++ 中,為什么我不能像這樣編寫 for() 循環(huán): for( int i = 1, double i2 = 0;)
    How does OpenMP handle nested loops?(OpenMP 如何處理嵌套循環(huán)?)
    Reusing thread in loop c++(在循環(huán) C++ 中重用線程)
    Precise thread sleep needed. Max 1ms error(需要精確的線程睡眠.最大 1ms 誤差)
    Is there ever a need for a quot;do {...} while ( )quot; loop?(是否需要“do {...} while ()?環(huán)形?)
    How to break out of a loop from inside a switch?(如何從交換機(jī)內(nèi)部跳出循環(huán)?)
    主站蜘蛛池模板: 中文字幕在线第二页 | 免费观看一级毛片 | 日韩成人高清在线 | 久久久久久国产 | 在线播放第一页 | 一区二区免费在线观看 | 日韩欧美专区 | 国产在线视频在线观看 | 91精品国产91久久久久久密臀 | 欧美性a视频 | 亚洲精品久久久一区二区三区 | 成人在线免费视频 | 久久久久国产精品午夜一区 | 日韩欧美在线视频播放 | 午夜理伦三级理论三级在线观看 | 亚洲品质自拍视频网站 | 国产男女视频网站 | 久久狠狠 | 国产精品久久久久久妇女 | 亚洲精品视频一区二区三区 | 综合精品久久久 | 国产aⅴ爽av久久久久久久 | 一区二区三区四区视频 | 久久久国产一区二区三区四区小说 | 日本亚洲精品 | 精品一区在线免费观看 | 91在线精品秘密一区二区 | 久久亚洲国产精品 | 日本精品一区二区三区在线观看视频 | 一区视频在线播放 | 亚洲一区久久久 | 国产日韩精品一区二区 | 国产精品久久久久久久久久久久冷 | 特黄一级 | 精品国产一区二区三区久久狼黑人 | 精品久久久久久久人人人人传媒 | va精品| 欧美最猛黑人xxxx黑人 | 激情视频网站 | 国产精品高清一区二区三区 | 欧美一级二级视频 |
  5. <rt id="uus8g"><delect id="uus8g"></delect></rt>
  6. <code id="uus8g"></code>
        <li id="uus8g"></li>
          <rt id="uus8g"><tr id="uus8g"></tr></rt>