明月笑刀无情
如果您真正在做的是处理CSV文件本身,那么Nelson的答案很有意义。但是,我怀疑CSV只是您要解决的问题的产物。在C ++中,这可能意味着您的数据模型如下所示: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。考虑到这一点,将CSV处理视为两个操作:// if you wanted to go nuts, you could use a forward iterator concept for both of theseclass CSVReader {public: CSVReader(const std::string &inputFile); bool hasNextLine(); void readNextLine(std::vector<:string> &fields);private: /* secrets */};class CSVWriter {public: CSVWriter(const std::string &outputFile); void writeNextLine(const std::vector<:string> &fields);private: /* more secrets */};void readCustomers(CSVReader &reader, std::vector &customers);void writeCustomers(CSVWriter &writer, const std::vector &customers);一次读取和写入一行,而不是保留文件本身的完整内存表示。有一些明显的好处:您的数据以对您的问题(客户)有意义的形式表示,而不是当前的解决方案(CSV文件)。您可以轻松地为其他数据格式添加适配器,例如批量SQL导入/导出,Excel / OO电子表格文件甚至HTML