问题描述
不言自明,我试过谷歌并得到了很多可怕的专家交流,我也在这里搜索无济于事.最好是在线教程或示例.谢谢各位.
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). CSVReaderandCSVWritercan 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 文件数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!
The End
相关推荐
Unable to access non-const member functions of objects in C++ std::set(无法访问 C++ std::set 中对象的非常量成员函数)...2024-08-14 C/C++开发问题17
Constructing std::function argument from lambda(从 lambda 构造 std::function 参数)...2024-08-14 C/C++开发问题25
Sync is unreliable using std::atomic and std::condition_variable(使用 std::atomic 和 std::condition_variable 同步不可靠)...2024-08-14 C/C++开发问题17
Why is overloading operatoramp;() prohibited for classes stored in STL containers?(为什么禁止对存储在 STL 容器中的类重载 operatoramp;()?)...2024-08-14 C/C++开发问题6
1LNK2038:检测到“RuntimeLibrary"不匹配:值“MT_StaticRelease" 2c++ 编译错误:ISO C++ 禁止指针和整数之间的比较 3CMake 找不到源文件 (add_executable) 4Cmake 链接库目标链接错误 5如何使用 VideoWriter 从 OpenCV 打开 GStreamer 管道 6CMakeLists.txt:30 (project) 中的 CMake 错误:找不到 CMAKE_C_COMPILER 7CMake 无法确定目标的链接器语言 8如何解决munmap_chunk():C++中的无效指针错误热门文章
热门精品源码
1多功能实用站长工具箱html功能模板 2多风格简历在线生成程序网页模板 3论文相似度查询系统源码 4响应式旅游景点宣传推广页面模板 5在线起名宣传推广网站源码 6酷黑微信小程序网站开发宣传页模板 7房产销售交易中介网站模板 8小学作业自动生成程序最新VIP资源
- 您的内存占用可能更小(取决于相对

大气响应式网络建站服务公司织梦模板
高端大气html5设计公司网站源码
织梦dede网页模板下载素材销售下载站平台(带会员中心带筛选)
财税代理公司注册代理记账网站织梦模板(带手机端)
成人高考自考在职研究生教育机构网站源码(带手机端)
高端HTML5响应式企业集团通用类网站织梦模板(自适应手机端)