接收文件并删除文件中的重复项
Taking in a file and removing the duplicates within the file
我有一个逗号分隔的文件,它的扩展名.csv,术语如下所示:
30,Movies,53808-0776,278,65.75
31,Beauty,48951-8130,725,83.42
32,Baby,59779-224,848,82
33,Industrial,55711-070,753,46.48
34,Industrial,76446-002,272,89.03
35,Sports,68151-2870,185,2.86
36,Toys,0245-0709,783,45.65
37,Games,49999-963,523,93.65
38,Beauty,52125-508,500,2.38
39,Toys,54092-381,783,45.65
40,Beauty,55154-6649,666,79.52
41,Jewelry,57664-327,46,10.28
42,Grocery,49738-453,317,29
我需要我的程序做的是接受这种格式的任何通用文件,删除重复项,然后创建一个没有重复项的相同扩展名的新文件。用户键入文件的确切位置。我正在使用一个类来保存单个数据记录。这就是我到目前为止的标题。
#ifndef RECORD_H_
#define RECORD_H_
#include <iostream>
class Record {
public:
// Constructor
std::Record(string department, string item_code, int quantity, double cost);
// Deconstructor
virtual ~Record();
// Overloaded '==' and '<' Comparision Methods
friend bool operator ==(const Record &a, const Record &b);
friend bool operator <(const Record&, const Record&);
// Overloaded '<<' Operator
friend std::ostream& operator <<(std::ostream&, const Record&);
// Private Member Functions
private:
std::string department;
std::string item_code;
int quantity;
double cost;
};
#endif /* RECORD_H_ */
这些文件都很小,因此可以使用简单的排序方法。我对这些方面感到困惑。我是否执行在源代码的向量中存储对象的操作,还是需要为此创建定义?另外,如果我接收一个文件,如何让程序以相同的扩展名创建新文件?(.csv)
假设您有一个具有定义operator==
的问题中描述的Record
类,并且您有一个从csv文件中读入内存的向量,您可以执行以下操作来使用算法库删除重复项:
std::vector<Record> records; // full of Record
// rearranges the vector, moving all the unique elements to the front
// and duplicates to the back
// last is an iterator to beginning of the duplicates
auto last = std::unique(records.begin(), records.end());
// erase all duplicates
records.erase(last, records.end());
注意 std::unique
将在 Record
中使用您定义的operator==
。
相关文章:
- 如何在我从C++文件中读取后立即删除行
- 删除 XML 文件中的子节点C++
- 如何在输入新内容 c++ 之前删除文件中的所有内容
- 如何更新和删除文本文件中的特定逗号分隔值?C++
- 为什么我的程序在读取/写入文件时会删除最重要的数字?
- 将内容从第一个文件("constituencies")移动到第二个文件("temp")并在之后重命名时,我的文件被删除
- 文件模式标志"ios::app"是否用于删除文件(如果文件已存在)?
- 如何自动获取我的项目的路径并删除一些文件
- C++ VS 错误:<实验/文件系统>提供 std::experimental::文件系统的标头已被Microsoft弃用,将被删除
- 如果我删除指针,我的C++程序会意外删除系统文件吗?
- 在 c++ 中打开文件、删除标点符号并追加到另一个文件中
- 文件删除(使用取消链接)与释放所有磁盘空间之间的延迟
- 重命名批处理文件(删除名称中的一些符号)(python/matlab/shell)
- 如何找到字符串并使用C 文件中的文件删除它
- 使用 DEF 文件删除 dll 导出的名称
- 关闭后等待文件删除
- c++中的文件删除
- 如何共享文件删除权限时,我打开一个文件ifstream
- 典型的Win32文件删除(IDropTarget::drop)实现
- 生成文件模式规则:循环生成文件.o < - 删除生成文件依赖项