問題描述
不言自明,我試過谷歌并得到了很多可怕的專家交流,我也在這里搜索無濟于事.最好是在線教程或示例.謝謝各位.
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::vector
或 std::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:
- 您的數據以對您的問題(客戶)有意義的形式表示,而不是當前的解決方案(CSV 文件).
- 您可以輕松地為其他數據格式添加適配器,例如批量 SQL 導入/導出、Excel/OO 電子表格文件,甚至 HTML
渲染.
- 您的內存占用可能更小(取決于相對
sizeof(Customer)
與單行中的字節數).CSVReader
和CSVWriter
可以作為內存模型(例如 Nelson 的)的基礎而重復使用,而不會損失性能或功能.反之則不然.- Your data is represented in a form that makes sense for your problem (customers), rather than the current solution (CSV files).
- 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. - Your memory footprint is likely to be smaller (depends on relative
sizeof(Customer)
vs. the number of bytes in a single row). CSVReader
andCSVWriter
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?(如何從交換機內部跳出循環?)- • cout<<調用它打印的...
- • 使用 GCC 在可執行文件中嵌入...
- • Qt5 靜態構建產生無法加載平...
- • 為什么從`std::async` 阻塞返回...
- • std::thread 通過引用調用復制構...
- • 理解 std::hardware_corruption_int...
- • 如何在 Visual Studio 調試器中顯...
- • 對模板函數的未定義引用...
- • 什么時候應該在 C++ 中使用“...
- • C++11 中 COW std::string 實現的合...
- • 模塊化算法和 NTT(有限域 DF...
- • 如何使用 QueryPerformanceCounte...
- • cout<<調用它打印的...
- • 使用 GCC 在可執行文件中嵌入...
- • Qt5 靜態構建產生無法加載平...
- • 為什么從`std::async` 阻塞返回...
- • std::thread 通過引用調用復制構...
- • 理解 std::hardware_corruption_int...
- • 如何在 Visual Studio 調試器中顯...
- • 對模板函數的未定義引用...
- • 什么時候應該在 C++ 中使用“...
- • C++11 中 COW std::string 實現的合...
- • 模塊化算法和 NTT(有限域 DF...
- • 如何使用 QueryPerformanceCounte...
掃碼點餐 門戶 驗證碼 廣告設計 商務合作 商城模板 bootstrap 進銷存系統 零售系統 ar 商城 視頻教程 微擎 o2o 分發系統 音樂 淘寶客 discuz模板 微小區 服裝設計 ai 資源 小米 刷單 3d 小游戲 交友 蜘蛛池 卡券 你畫我猜 虛擬幣 區塊鏈 視頻 全景 漫畫網 OElove 按鈕切換 博客 物流網站 游戲模板 svg jquery angular 360 動畫模板 攝影 動畫特效 在線客服 扁平 地板 域名停放 域名頁 canvas html5 小程序 thinkphp 視頻打賞 java視頻 挖礦網 養生網 帝國cms 微信程序 電影源碼 css3 訂單系統 微商 微擎微贊 蘋果cms 郵件群發 小說源碼 - 您的內存占用可能更小(取決于相對