目前我正在尝试读取文本文件并忽略其中的重复行。这是我到目前为止所做的代码示例
string filename;
cout<<"Please enter filename: "<<endl;
cin>>filename;
ifstream inFile(filename.data());
typedef std::map<std::string, int> line_record;
line_record lines;
int line_number = 1;
if(inFile.is_open())
{
while(std::getline(inFile, line))
{
line_record::iterator existing = lines.find(line);
if(existing != lines.end())
{
existing->second = (-1);
}
else
{
lines.insert(std::make_pair(line,line_number));
++line_number;
getline(inFile,line);
cout<<line<<endl;
noOfLine++;
}
}
}else
{
cout<<"Error opening file! Please try again!"<<endl;
}
cout<<"\n"<<noOfLine<<" record(s) read in successfully!\n"<<endl;
文本文件(下):
Point2D, [3, 2]
Line3D, [7, 12, 3], [-9, 13, 68]
Point3D, [1, 3, 8]
Line2D, [5, 7], [3, 8]
Point2D, [3, 2]
Line3D, [7, -12, 3], [9, 13, 68]
Point3D, [6, 9, 5]
Point2D, [3, 2]
Line3D, [70, -120, -3], [-29, 1, 268]
Line3D, [25, -69, -33], [-2, -41, 58]
Point3D, [6, 9, -50]
但我得到的结果是:
Line3D, [7, 12, 3], [-9, 13, 68]
Line2D, [5, 7], [3, 8]
Point3D, [6, 9, 5]
Line3D, [25, -69, -33], [-2, -41, 58]
Point3D, [6, 9, -50]
有什么帮助吗??谢谢!!