問題描述
不言自明,我試過谷歌并得到了很多可怕的專家交流,我也在這里搜索無濟(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::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>
.
考慮到這一點(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:
- 您的數(shù)據(jù)以對(duì)您的問題(客戶)有意義的形式表示,而不是當(dāng)前的解決方案(CSV 文件).
- 您可以輕松地為其他數(shù)據(jù)格式添加適配器,例如批量 SQL 導(dǎo)入/導(dǎo)出、Excel/OO 電子表格文件,甚至 HTML
渲染.
- 您的內(nèi)存占用可能更小(取決于相對(duì)
sizeof(Customer)
與單行中的字節(jié)數(shù)).CSVReader
和CSVWriter
可以作為內(nèi)存模型(例如 Nelson 的)的基礎(chǔ)而重復(fù)使用,而不會(huì)損失性能或功能.反之則不然.- 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.
這篇關(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)?)- • cout<<調(diào)用它打印的...
- • 使用 GCC 在可執(zhí)行文件中嵌入...
- • Qt5 靜態(tài)構(gòu)建產(chǎn)生無法加載平...
- • 為什么從`std::async` 阻塞返回...
- • std::thread 通過引用調(diào)用復(fù)制構(gòu)...
- • 理解 std::hardware_corruption_int...
- • 如何在 Visual Studio 調(diào)試器中顯...
- • 對(duì)模板函數(shù)的未定義引用...
- • 什么時(shí)候應(yīng)該在 C++ 中使用“...
- • C++11 中 COW std::string 實(shí)現(xiàn)的合...
- • 模塊化算法和 NTT(有限域 DF...
- • 如何使用 QueryPerformanceCounte...
- • cout<<調(diào)用它打印的...
- • 使用 GCC 在可執(zhí)行文件中嵌入...
- • Qt5 靜態(tài)構(gòu)建產(chǎn)生無法加載平...
- • 為什么從`std::async` 阻塞返回...
- • std::thread 通過引用調(diào)用復(fù)制構(gòu)...
- • 理解 std::hardware_corruption_int...
- • 如何在 Visual Studio 調(diào)試器中顯...
- • 對(duì)模板函數(shù)的未定義引用...
- • 什么時(shí)候應(yīng)該在 C++ 中使用“...
- • C++11 中 COW std::string 實(shí)現(xiàn)的合...
- • 模塊化算法和 NTT(有限域 DF...
- • 如何使用 QueryPerformanceCounte...
掃碼點(diǎn)餐 門戶 驗(yàn)證碼 廣告設(shè)計(jì) 商務(wù)合作 商城模板 bootstrap 進(jìn)銷存系統(tǒng) 零售系統(tǒng) ar 商城 視頻教程 微擎 o2o 分發(fā)系統(tǒng) 音樂 淘寶客 discuz模板 微小區(qū) 服裝設(shè)計(jì) ai 資源 小米 刷單 3d 小游戲 交友 蜘蛛池 卡券 你畫我猜 虛擬幣 區(qū)塊鏈 視頻 全景 漫畫網(wǎng) OElove 按鈕切換 博客 物流網(wǎng)站 游戲模板 svg jquery angular 360 動(dòng)畫模板 攝影 動(dòng)畫特效 在線客服 扁平 地板 域名停放 域名頁(yè) canvas html5 小程序 thinkphp 視頻打賞 java視頻 挖礦網(wǎng) 養(yǎng)生網(wǎng) 帝國(guó)cms 微信程序 電影源碼 css3 訂單系統(tǒng) 微商 微擎微贊 蘋果cms 郵件群發(fā) 小說源碼 - 您的內(nèi)存占用可能更小(取決于相對(duì)